inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.34k stars 719 forks source link

error TS2307: Cannot find module 'inversify' #154

Closed kuncevic closed 8 years ago

kuncevic commented 8 years ago

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('./'));
});
remojansen commented 8 years ago

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.

kuncevic commented 8 years ago

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

remojansen commented 8 years ago

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

remojansen commented 8 years ago
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/**'
];
kuncevic commented 8 years ago

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.

remojansen commented 8 years ago

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.

kuncevic commented 8 years ago

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.

remojansen commented 8 years ago

It could be that there is a conflict between:

 "exclude": [
    "node_modules",

And

 "files": [
      "node_modules/inversify/type_definitions/inversify/inversify.d.ts"
  ]
kuncevic commented 8 years ago

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

remojansen commented 8 years ago

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.

kuncevic commented 8 years ago

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

remojansen commented 8 years ago

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

kuncevic commented 8 years ago

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" />

remojansen commented 8 years ago

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.

kuncevic commented 8 years ago

OK great. Thanks again.