-
Notifications
You must be signed in to change notification settings - Fork 723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Missing required @inject or @multiInject annotation" even though it's there #1004
Comments
I am having the same problem with my test project, |
I created a demo repo of this for ease of reproducing. https://github.com/Roustalski/broken-inversify |
@Roustalski try to remove constructor and use property injection. I create PR to your repo. |
same issue.
|
I can confirm that issue, also that @projekt86 workaround works. Do you have any idea what is cause of this? |
if you using index.ts, and import multiple classes from it, get rid of it. |
maybe related to babel: #1007 |
Do you know if there is a solution for this issue? I'm using CRA and @Inject decorator works without constructors, as @projekt86 suggested, but I would like to be able to use constructors in my typescript classes, as I was doing before installing inversify |
@albertocorrales Use tsc or tsloader to transform ts into js if you can. If you must use babel, add this plugin. |
@csr632, thanks for your answer. I would like to continue using babel, as we have some plugins there to build our bundle. However, I tried to configure the babel plugin and I'm still having the runtime error:
Here I forked @Roustalski 's repo to configure babel plugin. I'm not sure if I'm missing something or it is just not working: |
Here is how I didn't wanted to eject so I added react-app-rewired (https://github.com/timarney/react-app-rewired) Here is the steps to configure react :
config-overrides.js : const { override, addDecoratorsLegacy, addBabelPlugin } = require('customize-cra')
module.exports = override(
addDecoratorsLegacy(),
addBabelPlugin('babel-plugin-parameter-decorator'),
)
I feel I'm close to fix this, anyone have a clue ? |
Same issue here. I cannot get a class from a container if this class contains a constructor that receive an argument. |
This is still happening, any news on what might be the problem? |
same issue for me in react. cra |
EDIT: |
@thomai-d Not exactly. With CRA, it doesn't work as it doesn't support it. With react-app-rewired, you can get decorators working. They work for me with mobx. I tried pretty everything and didn't get babel-plugin-parameter-decorator working, so for me, inversify works only for parameter-less constructors. Even the case when the injected thing is a class itself, which need no decorator doesn't work. |
After this, I can inject in the constructor and not as property. |
Strangely enough I only have this issue when running tests. I have the same setup as @kegi and it (mostly) works fine, even building the project works fine. However, running EDIT: I have to take that back, I get a |
Fixed the issue by adding more babel plugins into the test environment, my module.exports = {
env: {
test: {
plugins: [
'transform-require-context',
+ 'babel-plugin-transform-typescript-metadata',
+ 'babel-plugin-parameter-decorator',
],
},
},
}; Hope this helps. |
This solved the issue for me too, but I have no idea why... |
This seems to happen when you have a circular dependency issue, i.e: A requires B, B in turn requires (not via DI, just via an import) A. https://github.com/aackerman/circular-dependency-plugin can be a handy way of chasing it down. |
The cause of the issue is most likely a circular dependency between modules, and that's why index file with all the services/stores/modules can sometimes cause it (it depends on all the services which sometimes depend on other services. You can play with the order of the imports in the index and it might help, but it's not guaranteed. Here's a great blog post about it: https://medium.com/@catalin.luntraru/webpack-is-smart-enough-to-not-fall-for-circular-dependency-881578246aeb In my use case I solved it by a dynamic import to the module I know I have a circular reference with. |
My problem was that I've needed to add
|
Could someone inject dependencies at the constructor of a Mobx store? because I could achieve it only with property injection. Ortherwise I also get the "Missing required @Inject or @multiInject annotation" |
For those who want to use InversifyJS with Next.js - here is the configuration I had to adjust. tsconfig.json (if you are using TypeScript) Add the following properties to {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types": ["reflect-metadata"],
} babel.config.js Create Babel config file in project root directory and add this content: module.exports = {
presets: ['next/babel'],
plugins: [
'babel-plugin-transform-typescript-metadata',
['@babel/plugin-proposal-decorators', { legacy: true }],
],
} Install the following NPM modules as dev dependencies:
|
This fixed it for me. Thanks @georgwittberger-the-nu-company |
This is 9/10 times due to a circular dependency. In my case it was Service1 injecting Service2 in its constructor and Service2 injecting Service1 in its constructor. What a poor error message though. |
Closing as it's a bundler config issue, unrelated to inversify. |
Hey, I have the problem that I can't get the injects working. It always says
Missing required @inject or @multiInject annotation
. Event though I add this annotation it still complains (I don't have a circular dependency either).Expected Behavior
Injection should work.
Current Behavior
I'm trying to get inversify working together with the latest create-react-app. Unfortunately no inject is working like expected. I took a really small example to reproduce this issue:
Steps to Reproduce (for bugs)
create-react-app test --typescript
cd test
yarn add inversify reflect-metadata
yarn start
".
Environment
Node version: v10.11.0
create-react-app version: 2.1.1
package.json:
I'm not sure if I have a wrong configuration somewhere or if this is a problem in create-react-app. I appreciate your help :)
The text was updated successfully, but these errors were encountered: