My nodejs Typescript server has grown in plexity and now relies on a few classes defined in another .ts file. This has created a strange pilation problem:
tsc server.ts
piles everything fine. Butnode server.js
crashes at the first line that instantiates a class from the other .ts filetsc --out server.js server.ts
results in the error message: "module emit collides with emitted script"tsc --out serv.js server.ts
seems to work but actually piles everything EXCEPT server.ts. The code from the other files is there andnode serv.js
just returns without any output
I'm not the only one with this error, unfortunately the solution on codeplex doesn't work for me.
How do I use tsc
correctly ?
My nodejs Typescript server has grown in plexity and now relies on a few classes defined in another .ts file. This has created a strange pilation problem:
tsc server.ts
piles everything fine. Butnode server.js
crashes at the first line that instantiates a class from the other .ts filetsc --out server.js server.ts
results in the error message: "module emit collides with emitted script"tsc --out serv.js server.ts
seems to work but actually piles everything EXCEPT server.ts. The code from the other files is there andnode serv.js
just returns without any output
I'm not the only one with this error, https://typescript.codeplex./workitem/294 unfortunately the solution on codeplex doesn't work for me.
How do I use tsc
correctly ?
2 Answers
Reset to default 16Your server.ts
dependencies need to be modules that export their surface area with top-level export
directives, and server.ts
should load them using import
directives. The root cause here is that TypeScript has two different sorts of universes for pilation.
The first is the default one that you'd use for regular webpages, where some simple loader takes 1 or more source files in some fixed order and executes them in that order, and you're on your own for dependency ordering. This is called "program" pilation. In program pilation, you might do side-by-side pilation (a.ts => a.js, b.ts => b.js), or you might do concatenated pilation using --out
((a.ts + b.ts) => out.js).
In program pilation, you refer to your references using ///<reference>
tags. If those references are to source files (.ts
), they'll get concatenated in to the output if using --out
, or emitted as a side-by-side .js
file otherwise. If those references are to a declaration file (.d.ts
), you're basically saying you will be getting definitions for those files loaded via the external loader (i.e. a <script>
tag in the browser).
The second is the kind of pilation you'd use for node.js or other environments that do asynchronous or idempotent module loading with runtime dependency resolution. This called "module" pilation. Here, the --module
flag you pass to tsc
matters, and the only valid thing to do is side-by-side pilation, because loading a single file as a module is (generally) how the module loaders in node.js, etc work.
In module pilation, you use the export
keyword on a top-level object (function, class, module, interface, or var) to control what's available to code that refers to you using import
. You should only ever have /// <reference>
tags that point to .d.ts
declaration files, because the module-based runtime loaders don't have a notion of loading a naked JS file. You won't pile with --out
.
You never want to mix and match these pilation modes, because it simply is not going to work. In fact, in 0.8.2.0, tsc
will simply issue an error if you try to do this.
I have a web app with a lot of TypeScript files. Here is how I solved this problem:
- Created a global _references.ts file in the ~/Scripts dir. This file has a reference path=... for each ts file in the web project.
- Next I generated this file using a T4 template because its was being a pain to manage by hand.
- Also, using the T4 template, I ordered the references in _references.ts according to my TypeScript dependencies. e.g. All ..Base.ts files at the top.
- Also, I created a tsc argument file which starts with -out app.js followed by a list of every ts file in the project. This was also generated using T4 and I called it app.tsproj.
- Finally, I call tsc @app.tsproj to generate the JavaScript with all dependencies correctly ordered.
_references.ts T4 template: https://gist.github./danabenson/5224712
app.tsproj T4 template: https://gist.github./danabenson/5224718