Search code examples
node.jstypescriptnpmnpm-installtsc

How to resolve Chicken/Egg situation with `tsc` and `npm install`?


So I have the standard folder structure

dist/
src/

where src has my .ts files and dist has my .js files. (I have "outDir":"dist" in my tsconfig.json file, and "includes" set to 'src').

Note that 'dist' is in my gitignore file, so it is not in version control, and so when it goes to Travis or CircleCI, nothing is in the dist folder until I run tsc.

Here is the problem - if I run npm install first - it will fail because I have this in my package.json:

"bin":{
  "foo" :"dist/cli.js"   // dist/cli.js does not exist yet
}

but if I run tsc first - tsc will then be missing dependencies that it needs for compilation, which arrive if I run npm install.

The only thing I can think of to solve this, is to install all the necessary tsc dependencies first, then run tsc, then run npm install --production.

However that is not the most convenient thing to do.

Has anyone run into this problem and found a good solution?


Solution

  • I don't remember having this problem but in at least one case I did something that will work around the issue.

    I put an index.js in the root folder that runs the actual dependency in dist. Then the bin that npm looks for is a file that's present, and it shouldn't freak out.

    It won't work until tsc is run, of course. But it should resolve your chicken and egg problem.