Ts-node: Use ts-node locally from node_modules

Created on 19 Dec 2017  路  6Comments  路  Source: TypeStrong/ts-node

Hi,

I there a way to use ts-node cli tool locally (from node_modules) ?
I would like to be able to run node_modules/.bin/ts-node src/index.ts instead of using it globally.

This will help to manage deferent ts-node versions for deferent projects.

Most helpful comment

I have found the solution in any case.

$ node_modules/ts-node/dist/bin.js src/index.ts

I didn't noticed that the "bin" file is located in the dist directory.
I think that a "bin" file in the node_modules/.bin directory will be a bit more user friendly, but its semantics at this point.

All 6 comments

You can create an npm script in your project as follow:

{
  "scripts": {
    "ts-node": "ts-node"
  }
}

And then call

npm run ts-node

Only drawback of this method is that you need to escape flags:

npm run ts-node -- --v8-flag-or-something

However, if you do not use flags like that (just a file name, for instance), you can simply do:

npm run ts-node my-filename.ts

Hope this helps.

@stelcheck thanks for the help but the idea is to be able to run the ts-node from a project local node_modules.

the script is only an alias and running ts-node in the cli will look for it in the global scope.

I have found the solution in any case.

$ node_modules/ts-node/dist/bin.js src/index.ts

I didn't noticed that the "bin" file is located in the dist directory.
I think that a "bin" file in the node_modules/.bin directory will be a bit more user friendly, but its semantics at this point.

According to https://docs.npmjs.com/cli/run-script :

In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix.

Since ts-node will be in that folder, what I suggested should work fine. I personally use this strategy on nunerous project, and have never run into any sort of issues.

As another option, npx can run project-local binaries. This may be easier than using full node_modules/*/dist/bin.js paths or setting up scripts entries in package.json.

but this still needs typescript globally installed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

grissius picture grissius  路  3Comments

KiaraGrouwstra picture KiaraGrouwstra  路  3Comments

motss picture motss  路  4Comments

conordickinson picture conordickinson  路  4Comments

sodiumjoe picture sodiumjoe  路  4Comments