Search code examples
typescriptnpmnpm-installtsc

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 !


Solution

  • 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.