0

When I try to import a local file it either throws an error at runtime but compiling it using tsc works. When I make it work for node.js, typescript throws an error at compile time.

When I do

import A = require("./A");

node.js complains, that it can not find the A module and typescript compiles just fine.

But when I change it to

import A = require("./js/A");

node.js can find the module but Typescript tells me there is an error.

The layout is like this:

js
\- A.ts
\- B.ts

I compile the files separately and I already tried searching for changing the root directory for the typescript compiler but I couldn't find anything.

WebFreak001
  • 2,415
  • 1
  • 15
  • 24
  • Are you trying to import A into B? If so you should do `import A = require("A");` – Martin Apr 22 '15 at 20:24
  • @Martin if I do that typescript compiles but then the node.js code doesn't work because node.js expects internal modules to be paths relative to the root of the directory ("./js/A"), but typescripts expects it relative to the typescript file ("./A") – WebFreak001 Apr 22 '15 at 20:27

1 Answers1

1

Without seeing how you are compiling the TypeScript, and without seeing how you are attempting to require() these files from node, it's hard to answer this question.

Even so, I can tell you that both TypeScript and node.js are expecting require() to be given a path which is relative to the file doing the require.

If I were to have a js/A.ts that looked like so:

import B = require("./B");
console.log(B.thing);

And a js/B.ts that looked like so:

var myStuff = {
    thing: "I'm a thing!"
}
export = myStuff;

I could then compile both files with a single tsc -m commonjs ./js/A.ts

And then I could run node with: node ./js/A.js and would see the output:

I'm a thing!
thetoast
  • 763
  • 4
  • 9
  • Ok just tried, vanilla nodejs works fine but using node-webkit it doesn't work. https://github.com/nwjs/nw.js/wiki/Differences-of-JavaScript-contexts – WebFreak001 Apr 23 '15 at 12:19
  • @WebFreak001 how are you loading the js files in node-webkit? You still have to use `require()` for those. If you put the path to the TypeScript compiled js into a ` – thetoast Apr 23 '15 at 12:48
  • yeah i did that, changed it to require now – WebFreak001 Apr 23 '15 at 13:14