TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Cannot find module from ./node_modules

Open c9s opened this issue 10 years ago • 5 comments

Don't know if this is a expected bahaviour ? I thought loading node modules should be OK?

TypeScript Version:

1.8.0, 1.8.0-beta, also in 1.9.0-dev.20160217

Install

 npm install object-assign

so we will have node_modules/object-assign/index.js installed.

Config

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "allowJs": true
  },
  "files": ["entry.ts"]
}

Code

import * as assign from "object-assign";

Expected behavior:

should load the module from node_modules/object-assign

Actual behavior:

error TS2307: Cannot find module 'object-assign'.

c9s avatar May 25 '16 12:05 c9s

typings install object-assign

And change import statement to

import assign from "object-assign";

solves this problem.

c9s avatar May 25 '16 12:05 c9s

Is there any way to ignore error TS2307 in case where typings were not available for package installed from npm?

marcinvalas avatar Aug 26 '16 15:08 marcinvalas

declare module "the-module-name";

Which package is missing type definitions? I'm working on a tool to generate these quasi-automatically and am interested to try it out

RyanCavanaugh avatar Aug 26 '16 15:08 RyanCavanaugh

Thanks for quick response!

Should I declare it in typings\globals\[the-module-name].d.ts ? For sure, your one line solution produce error TS1005: '{' expected.. I assume that your answer is an shortcut for writing whole typings for this module, am I right?

I am searching for quick solution for situation where I can not find typings, writing them from scratch is not what I expect.

I can not find typings for react-simple-dropdown and react-cookie.

marcinvalas avatar Aug 26 '16 15:08 marcinvalas

Sorry, that syntax is specific to TS2.0.

It doesn't matter where you place the file as long as it's part of your compilation.

In pre-2.0 you can write the equivalent

declare module "react-cookie" { 
  var x: any;
  export = x;
}

RyanCavanaugh avatar Aug 26 '16 17:08 RyanCavanaugh