Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Reflect.hasOwnMetadata is not a function

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"
  ]
}

Could someone please review and let me know what I could 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.

like image 476
user1790300 Avatar asked Mar 02 '18 23:03

user1790300


2 Answers

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";
like image 118
Remo H. Jansen Avatar answered Sep 24 '22 23:09

Remo H. Jansen


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

like image 37
Sharet. N Avatar answered Sep 24 '22 23:09

Sharet. N