Guy
Guy

Reputation: 13336

Typescript + nodeJS: import of fs becomes string

I'm new with Typescript and with NodeJS.

For some reason this:

GetMenuDataCommand.ts

"use strict";

import fs = module("fs")

becomes this:

GetMenuDataCommand.js

  var fs = "fs";

Typescript - i love you - but why?

Upvotes: 4

Views: 10897

Answers (4)

Rick
Rick

Reputation: 13510

You are missing types.

From the command line.

npm install typings --save-dev
node node_modules/typings/dist/bin.js install dt~node --save --global

You must include the global flag or else your text editor won't pick up the .dt.ts file.

Upvotes: 0

Fenton
Fenton

Reputation: 251242

I'm using TypeScript 0.9 the following code:

/// <reference path="node.d.ts" />

import fs = require("fs");

fs.appendFile('name.txt', 'Some data');

Produces the following identical output:

var fs = require("fs");

fs.appendFile('name.txt', 'Some data');

(In CommonJS mode - in AMD mode it does the below...)

define(["require", "exports", "fs"], function(require, exports, __fs__) {
    var fs = __fs__;

    fs.appendFile('name.txt', 'Some data');
});

Upvotes: 4

Nathan Ridley
Nathan Ridley

Reputation: 34426

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.

Upvotes: 1

basarat
basarat

Reputation: 276303

Not sure if its related but in ts 0.9.x use require instead of module keyword :

"use strict";

import fs = require("fs")

Upvotes: 0

Related Questions