Skip to content

Files

Latest commit

56ca059 · Sep 20, 2016

History

History
22 lines (11 loc) · 1.17 KB

nodejs.md

File metadata and controls

22 lines (11 loc) · 1.17 KB

TypeScript with NodeJS

TypeScript has had first class support for NodeJS since inception. Here's how to get setup with a NodeJS project in TypeScript:

  1. Compile with --module set to "commonjs" (as we mentioned in modules)
  2. Add node.d.ts (typings install dt~node --global) to your compilation context.

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!

Creating TypeScript node modules

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 typings field (e.g. src/index) in your package.json similar to the main field to point to the default TypeScript definition export. For an example look at package.json for csx.

Example package: npm install csx for csx, usage: import csx = require('csx').

Bonus points

Such NPM modules work just fine with browserify (using tsify) or webpack (using ts-loader).