David Gustavsson
David Gustavsson

Reputation: 597

VS 2015 cannot find module, but cli node works

I just started working with TypeScript in VS2015, and it have gone very well so far. I have set up a structure, and it was all compiling and doing as expected.

But the problem started when I tried to include npm-installed modules.

This works without problem:

import * as fs from 'fs';

fs is available by default. But when I use npm to install additional modules (in this case xml2js), VS 2015 claims to not recognized the module.

import * as xml2js from 'xml2js'; 

...

Error TS2307 Cannot find module 'xml2js'.

But the funny thing is that if I go to the project folder and run the cli command

node app.js

it compiles without error. So it seems as VS 2015 does not recognize the modules, even if the package.json contains the correct packages.

Another thing, in VS 2015 solution viewer, the packages are listed, but are marked with a red circle, see below:

Note the red circles on the packages in the npm tree

Does anyone know what the red circles indicate?

I have probably done a bobo somewhere, or skipped a crucial part, but I am not man enough to successfully Google it.

Any help is appreciated.

Upvotes: 0

Views: 274

Answers (1)

David Gustavsson
David Gustavsson

Reputation: 597

Okay, thanks to Juan's comment, I was able to solve my issue. The solution is easy, when you know how TypeScript and modules work.

In short, modules are usually developed with js in mind. When you start using TypeScript, the modules you plan to use need to be developed with TypeScript in mind (see Juan's comment regarding typings, .d.ts files). One good example that has been converted to match this is the module moment.

But for others, less utilized libraries, you might need to complement the ordenary module with a DefinitelyTyped module (DefinitelyTyped)

Here you will find support for most of the different modules available. This will download a TS complement for the intended module, allowing you to import the module as usual. More detailed information can be found on the link above.

Upvotes: 1

Related Questions