Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use tsc without npm install -g

I'm working on a project that I have to give to some guys. The project is in typescript and so they have to run the command tsc to compile.

The problem is when I run this command after doing npm install typescript it seem that the command doesn't exist but if I install with the -g option then it works.

But I really would like the possibility to the futur owner of the project to just have to run npm install without installing dependencies on his computer.

But is it even possible ?

btw I'm on ubuntu 20

Thanks !

like image 959
Thibaud Avatar asked Oct 18 '25 17:10

Thibaud


2 Answers

npm install typescript installs it locally to the project. I would go further and add --save-dev so it only gets listed under devDependencies, not dependencies.

You can still run TypeScript in that situation. Your best bet is to add entries to the "scripts" key in package.json, like this:

{
    "name": "blah",
    "version": "1.0.0",
    "description": "...",
    "scripts": {
        "build": "tsc command arguments go here"
    },
    "keywords": [],
    "author": "...",
    "license": "...",
    "devDependencies": {
        "typescript": "^4.5.5"
    }
}

Then you run that command via:

npm run build

Node.js will find tsc in node_modules and run it.

like image 186
T.J. Crowder Avatar answered Oct 20 '25 07:10

T.J. Crowder


They can run it from the node_modules folder too.

./node_modules/typescript/bin/tsc

Or if you don't like that you could use npx which runs binaries from local node_modules folder.

npx tsc
like image 31
MWO Avatar answered Oct 20 '25 08:10

MWO