Guy Guy - 1 month ago 5x
Node.js Question

Typescript + nodeJS: import of fs becomes string

I'm new with Typescript and with NodeJS.

For some reason this:


"use strict";

import fs = module("fs")

becomes this:


var fs = "fs";

Typescript - i love you - but why?


Are you referencing the node.d.ts file correctly? If TypeScript doesn't have a reference to the definition file for your import, it'll generate a string for the import instead of the expected code.

For example, I had the following:

import passport = require('passport');

and that was generating:

var passport = "passport";

Turns out I'd forgotten to reference the definition file. Adding a reference at the top of the file solved the problem:

/// <reference path="../../definitions/passport.d.ts" />

Arguably you should spot it easily because the TypeScript compiler would throw an error, but 0.9.x is somewhat bug-ridden and it doesn't always catch everything.