23

I am trying to set up a TypeScript express/node application in Visual Studio Code following the Microsoft guide but changing it to use TypeScript however when it comes to installing the type definitions using typings I seem to have to install more packages than the guide.

I'm running the following pair of commands:

typings install node --ambient --save
typings install express --ambient --save

However attempting to build with just those packages gives the following type of error:

error TS2307: Cannot find module 'serve-static'.

For the following typings:

  • mime
  • express-serve-static-core
  • serve-static

I can resolve this by installing the required typings but it seems like something typings should do by itself.

I wanted to check if I was missing a fundamental step to automatically pull in dependencies or whether the guide was outdated?

In case it's relevant, my tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "sourceMap": true,
        "outDir": "bin",
        "sourceRoot": "src"
    },
    "exclude": [
        "node_modules",
        "typings/browser.d.ts",
        "typings/browser"
    ]
}

My tsc is version 1.8.7 and I have typescript installed globally.

Underscore
  • 1,017
  • 2
  • 10
  • 26
  • 1
    While it would be great if it pulled the ambient declarations today, it currently doesn't and it likely won't change. See [this issue](https://github.com/typings/typings/issues/113) as well as [this comment](https://github.com/typings/typings/issues/281#issuecomment-192551121) – chrisbajorin Mar 15 '16 at 17:30
  • Just a side note that "ambient" is deprecated now: `typings ERR! deprecated The "ambient" flag is deprecated. Please use "global" instead` – Musa Haidari Aug 23 '16 at 10:06

3 Answers3

31

As of the release of TypeScript 2.0 last month, the recommended tool for installing typings is our trusty old friend npm instead of typings or tsd.

npm install @types/node --save

With npm, there's no need to worry about "global" or "ambient" installations anymore.

You also don't need to worry about adding <reference> tags to the top of your source files anymore; just drop the following property into your compilerOptions in tsconfig.json, and the TypeScript compiler will automatically find the npm typings you've installed:

"typeRoots": [ "node_modules/@types" ]

Here's a blog post that explains the change in some more detail: https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/

Josh1billion
  • 14,837
  • 8
  • 38
  • 48
  • 5
    `@types`modules are defined as the default for typedeclarations in typescript 2. For this reason `"typeroots" : [ "node_modules/@types` ]` is unnecessary as long as it is the only typing folder. – Samuel Kupferschmid Oct 26 '16 at 12:00
  • 1
    This is incredible! What a life-saver. Typings was become quite the pain in the arse – Angad Nov 05 '16 at 07:58
  • 1
    I haven't had any time to look at TypeScript for a long while but this seems to work for me now. I've changed this to the accepted answer. – Underscore Nov 06 '16 at 12:17
8

The tutorial I linked has now been updated to include the following commands:

typings install node --ambient
typings install express serve-static express-serve-static-core --ambient

See @cdbajorin 's comment for information about why dependencies are not automatically downloaded.

Community
  • 1
  • 1
Underscore
  • 1,017
  • 2
  • 10
  • 26
  • 8
    _--ambient_ has been renamed to _--global_ in [typings-1.0.0](https://github.com/typings/typings/releases/tag/v1.0.0) – Stewart Francis May 17 '16 at 13:55
  • 2
    Also (as far as I can make out) the default repository for installation is now npm. There's a new repository added called env which contains type declarations for environments (such as node). I used: `typings install env~node@4.0.0 --save --global` Not sure if the situation with the global dependencies of your dependencies has changed with this. – Stewart Francis May 17 '16 at 13:59
2

to save everyone a headache, the magic command to get the typings for node is now:

typings install node --source env --global --save

Will Munn
  • 7,363
  • 4
  • 27
  • 32