Reputation: 21765
I'm using Visual Studio 2013 (Update 3), Node.js Tools v1.0.20721.02 and Node.js v0.10.31
I'm trying to put each class into its own file.
At design time everything seems fine, intellisense is working and the code compiles without issues.
At runtime however, node tells me it cannot find the classes.
I've reproduced this again and again by creating a new Node console project in Visual Studio
SomeClass.ts
export class SomeClass
{
name: string;
constructor(name: string)
{
this.name = name;
}
}
app.ts
///<reference path="Scripts/typings/node/node.d.ts"/>
import some = require("SomeClass");
var instance = new some.SomeClass("Batman");
console.log(instance.name);
The generated javascript output looks like this:
SomeClass.js
var SomeClass = (function () {
function SomeClass(name) {
this.name = name;
}
return SomeClass;
})();
exports.SomeClass = SomeClass;
//# sourceMappingURL=SomeClass.js.map
app.js
///<reference path="Scripts/typings/node/node.d.ts"/>
var some = require("SomeClass");
var instance = new some.SomeClass("Batman");
console.log(instance.name);
//# sourceMappingURL=app.js.map
Runtime output
module.js:340
throw err;Error: Cannot find module 'SomeClass'
at Function.Module._resolveFilename (module.js:338:15)
...
Visual Studio Solution structure
This is a standard solution created by the project template in Visual Studio, shouldn't it just work out of the box?
I've seen quite a few related questions, including this one, which seem to solve the
the issues most people are having, but don't seem to solve my problem.
Upvotes: 0
Views: 245
Reputation: 1985
Try using require("./SomeClass")
instead:
node.js resolves require
differently when there is no path prepended (see here), so in this case you need to tell it to look in the current directory, not for a core module or inside an (inexistent) node_modules
directory.
Further info on why it does not fail before runtime, given by the OP in the comments:
Apparently VS resolves everything whether you prepend './' or not and at runtime node requires './' to be prepended.
Upvotes: 1
Reputation: 17377
Run your program again adding the following line before your require:
console.log("cwd:%s", process.cwd());
And adjust the path you require so it starts at your current working directory (cwd).
Upvotes: 0