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

前端 未结 22 1917
时光说笑
时光说笑 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:10

    Include coffee-script in package.json with the specific version required in each project, typically like this:

    "dependencies":{
      "coffee-script": ">= 1.2.0"
    

    Then run npm install to install dependencies in each project. This will install the specified version of coffee-script which will be accessible locally to each project.

    0 讨论(0)
  • 2020-11-22 13:11

    For Windows use this:

    /* cmd into "node_modules" folder */
    "%CD%\.bin\grunt" --version
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 13:16

    UPDATE: As Seyeong Jeong points out in their answer below, since npm 5.2.0 you can use npx [command], which is more convenient.

    OLD ANSWER for versions before 5.2.0:

    The problem with putting

    ./node_modules/.bin
    

    into your PATH is that it only works when your current working directory is the root of your project directory structure (i.e. the location of node_modules)

    Independent of what your working directory is, you can get the path of locally installed binaries with

    npm bin
    

    To execute a locally installed coffee binary independent of where you are in the project directory hierarchy you can use this bash construct

    PATH=$(npm bin):$PATH coffee
    

    I aliased this to npm-exec

    alias npm-exec='PATH=$(npm bin):$PATH'
    

    So, now I can

    npm-exec coffee
    

    to run the correct copy of coffee no matter of where I am

    $ pwd
    /Users/regular/project1
    
    $ npm-exec which coffee
    /Users/regular/project1/node_modules/.bin/coffee
    
    $ cd lib/
    $ npm-exec which coffee
    /Users/regular/project1/node_modules/.bin/coffee
    
    $ cd ~/project2
    $ npm-exec which coffee
    /Users/regular/project2/node_modules/.bin/coffee
    
    0 讨论(0)
  • 2020-11-22 13:16

    Use the npm bin command to get the node modules /bin directory of your project

    $ $(npm bin)/<binary-name> [args]
    

    e.g.

    $ $(npm bin)/bower install
    
    0 讨论(0)
  • 2020-11-22 13:16

    zxc is like "bundle exec" for nodejs. It is similar to using PATH=$(npm bin):$PATH:

    $ npm install -g zxc
    $ npm install gulp
    $ zxc which gulp
    /home/nathan/code/project1/node_modules/.bin/gulp
    
    0 讨论(0)
提交回复
热议问题