7

I am trying to use inversify with typescript and node.js. I am currently using node.js version 6.9.1, typescript version 2.6.2, and ECMAScript 6. When I try to run the node.js application, I keep receiving the following error, "TypeError: Reflect.hasOwnMetadata is not a function".

Following documentation I found online, by adding import "reflect-matadata" at the top of the inversify.config.js file, it seemed to work for awhile, but now the problem is back again.

In my inversify.config.ts file, I have the following code that gets called from my app.ts file:

export let initialize = async (): Promise<Container> => {
        var container = new Container();

        try {
                if (!Reflect.hasOwnMetadata("inversify:paramtypes", ClassToAddInjectableTo)) {
                        decorate(injectable(), ClassToAddInjectableTo);
                }
                
                ….

            container.bind<interfaces.Controller>(TYPE.Controller).to(MyController1).whenTargetNamed('MyController1');
            container.bind<interfaces.Controller>(TYPE.Controller).to(MyController2).whenTargetNamed('MyController2');
        } catch (ex) {
            throw ex;
        }
}

This code exists in app.ts and it calls the code in the inversify.config.ts file:

setup = async () => {
    try {
        let container: Container = await initialize();
        …..

    } catch (err){
        throw err;
    }
}

The problem seems to be on the following line in node_modules/inversify/lib/annotation/decorator_utils.js:22:

if (Reflect.hasOwnMetadata(metadataKey, annotationTarget) === true) {

In the generated inversify.config.js file, the following code seems to be above the import "reflect-metadata":

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");

I noticed that the help I found online only indicated that I needed to add import "reflect-metadata".

Also, here is my tsconfig.json file:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "ES6",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "types": ["reflect-metadata"],
    "lib": ["ES6"],
    "sourceMap": true,
    "inlineSources": true,
    "pretty": true,
    "outDir": "dist",
    "rootDir": "src",
    "noLib": false,
    "declaration": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

What could I be missing?

Update

For the issue above, I added the import "reflect-metadata" to the file inversify.config.ts file. However, I commented this import statement and added the import "reflect-metadata" to the app.ts file and now I am getting a different error:

throw new Error(ERRORS_MSGS.DUPLICATED_INJECTABLE_DECORATOR);
            ^

Error: Cannot apply @injectable decorator multiple times.

Now I found a post on the internet that seemed to describe indicate the adding import "reflect-metadata" adds a Reflect global variable. Also, I don't know if this helps but I removed the @injectable decorator from the controllers.

halfer
  • 19,824
  • 17
  • 99
  • 186
user1790300
  • 2,143
  • 10
  • 54
  • 123
  • Provide more context, minimal reproducing example. Evident, that [reflect-metadata](https://www.npmjs.com/package/reflect-metadata) pollyfill does not included correctly somewhere. Transpiled code you has provided not interesting - it's just TypeScript helper definition, added to every file. – Pavel Mar 03 '18 at 07:11
  • Add `lib: [ "es2015.reflect" ]` – unional Mar 08 '18 at 01:01
  • Are you downloading the `reflect-metadata` library? It looks like you have not `npm install`-ed it. – Andrew Eisenberg Mar 09 '18 at 21:15
  • @unional. Do I add the lib enty next to the one for es6, comma-separated? – user1790300 Mar 11 '18 at 01:12
  • Add that to your `lib:[]` and see if that solves your problem. – unional Mar 11 '18 at 01:13

2 Answers2

15

Try:

npm install reflect-metadata --save

Then import it only once in your entire application:

import "reflect-metadata"

If you are using inversify-express-utils make sure that your controllers are annotated with @controller not with @injectable. Also make sure that your coontrollers are imported once.

import "./controllers/some_controller";
import "./controllers/another_controller";
Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93
5

In case of failure during running jest test, add to the top of the spec file: import "reflect-metadata";

Sharet. N
  • 51
  • 1
  • 2