TypeScript has had first class support for NodeJS since inception. Here's how to get setup with a NodeJS project in TypeScript:
- Add
node.d.ts(npm install @types/node --save-dev) to your compilation context. - Compile with
--moduleset to"commonjs". - Add node to the global resolution by simply adding it to
typesin your tsconfig.
So your tsconfig will look like:
{
"compilerOptions": {
"module": "commonjs",
"types": [
"node"
]
}
}That's it! Now you can use all the built in node modules (e.g. import fs = require('fs')) with all the safety and developer ergonomics of TypeScript!
You can even use other node modules written in TypeScript. As a module author, one real thing you should do:
- you might want to have a
typingsfield (e.g.src/index) in yourpackage.jsonsimilar to themainfield to point to the default TypeScript definition export. For an example look atpackage.jsonfor csx.
Example package: npm install csx for csx, usage: import csx = require('csx').
Such NPM modules work just fine with browserify (using tsify) or webpack (using ts-loader).