Inversifyjs: error TS2307: Cannot find module 'inversify'

Created on 5 Apr 2016  路  15Comments  路  Source: inversify/InversifyJS

When import { TypeBinding, Kernel } from 'inversify'; getting an error TS2307: Cannot find module 'inversify'.

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "watch": true
  },
  "exclude": [
    "node_modules",
    "typings/browser.d.ts",
    "typings/browser"
  ]
}

gulp task:

var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var ts = require('gulp-typescript');

var path = ["**/*.ts", "!node_modules/**", "!typings/browser/**", "!typings/browser.d.ts", '!bin/**'];
var config = ts.createProject('./tsconfig.json');

gulp.task('ts', ['clean'], function() {
    return gulp
        .src(path, {base: './'})
        .pipe(sourcemaps.init())
        .pipe(ts(config))
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('./'));
});
question

All 15 comments

Looks like you forgot to add a reference to the type definition file:

/// <reference path="node_modules/inversify/type_definitions/inversify/inversify.d.ts" />

Once we go into beta we will distribute this file via typings but for the moment you need to manually add the reference. Please let me know if it works.

@remojansen Oh I see. Is there any way to avoid this line and just reference is sort of globally?

You should be able to add it to your path in your gulp configuration.

var path = [
    "**/*.ts",
   "node_modules/inversify/type_definitions/inversify/inversify.d.ts",
    "!node_modules/**", // maybe you need to remove this??
    "!typings/browser/**",
    "!typings/browser.d.ts", '!bin/**'
];

ehhh vscode for some reason saying can't find module inversify without this thing /// <reference path="node_modules/inversify/type_definitions/inversify/inversify.d.ts" /> in the top of .ts file.

I think I know whats going on... VS Code uses the tsconfig.json file not the gulpfile.js, so instead of declaring the files to be included in your gulpfile.js you can declare then in your tscondig.json file. You can see an example here.

That is how my typescript.json looking like:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "watch": true
  },
  "exclude": [
    "node_modules",
    "typings/browser.d.ts",
    "typings/browser"
  ],
  "files": [
      "node_modules/inversify/type_definitions/inversify/inversify.d.ts"
  ]
}

UPDATE:
From https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md
NOTE: exclude should not be used when files or filesGlob are in use. The presence of the files property takes presedence over the exclude property.

It could be that there is a conflict between:

 "exclude": [
    "node_modules",

And

 "files": [
      "node_modules/inversify/type_definitions/inversify/inversify.d.ts"
  ]

That is tricky bit. Just having this section:

  "files": [
      "node_modules/inversify/type_definitions/inversify/inversify.d.ts",
      "typings/main.d.ts"
  ]

Still having same issue

UPDATE:
Just installed inversify typings to default folder typings install inversify --ambient --save
Then rollback my exclude section

  "exclude": [
    "node_modules",
    "typings/browser.d.ts",
    "typings/browser"
  ]

And now vscode is happy.
Also had ts build issue error TS2304: Cannot find name 'Promise'. which fixed by typings install bluebird --ambient --save

I don't know :disappointed: I would suggest as a temporal solution that you add the reference:

/// <reference path="../node_modules/inversify/type_definitions/inversify/inversify.d.ts" />

To your main.d.ts file. Once we go into beta you will be able to install the inversify.d.ts file using typings so all you will need to do is to remove the reference.

@remojansen I just did install inversify using typings :) see my prev comment.
All good now and thanks for your help :+1:

The problem is the versions. Which version of InversifyJS are you using?

Ah I see the trick. I've got typings 2.0.0-alpha.3 installed but I need 2.0.0-alpha.6 which is in <reference path="../node_modules/inversify/type_definitions/inversify/inversify.d.ts" />

Yes that's the problem. I just opened a PR https://github.com/DefinitelyTyped/DefinitelyTyped/pull/8851 to solve your problem. I recommend you to use the latest 2.0.0-alpha.8 and to keep an eye on releases. We will probably have a couple of releases this month before going into beta.

OK great. Thanks again.

Was this page helpful?
0 / 5 - 0 ratings