How to use executables from a package installed locally in node_modules?

前端 未结 22 2180
时光说笑
时光说笑 2020-11-22 12:40

How do I use a local version of a module in node.js. For example, in my app, I installed coffee-script:

npm install coffee-script
22条回答
  •  眼角桃花
    2020-11-22 13:13

    If you want to keep npm, then npx should do what you need.


    If switching to yarn (a npm replacement by facebook) is an option for you, then you can call:

     yarn yourCmd
    

    scripts inside the package.json will take precedence, if none is found it will look inside the ./node_modules/.bin/ folder.

    It also outputs what it ran:

    $ yarn tsc
    yarn tsc v0.27.5
    $ "/home/philipp/rate-pipeline/node_modules/.bin/tsc"
    

    So you don't have to setup scripts for each command in your package.json.


    If you had a script defined at .scripts inside your package.json:

    "tsc": "tsc" // each command defined in the scripts will be executed from `./node_modules/.bin/` first
    

    yarn tsc would be equivalent to yarn run tsc or npm run tsc:

     yarn tsc
     yarn tsc v0.27.5
     $ tsc
    

提交回复
热议问题