NPM : how to source ./node_modules/.bin folder?

后端 未结 8 1889
慢半拍i
慢半拍i 2021-02-19 14:23

I have a problem on npm installation

I have created a project say project A

cd ~/projectA
npm install sails

but sails command is not fo

相关标签:
8条回答
  • 2021-02-19 14:29

    If you don't like to mess up with your PATH for running a npm script that isn't global -- e.g. you are the only one to use it --, I would personally recommend the use of an sh "alias".

    1. npm install (locally) your beloved package (json-diff here, for instance)

      cd ~ && npm install json-diff
      
    2. alias it (save it in your ~/.xxxxrc file):

      alias diffj "\`npm bin\`/json-diff !*"
      

    Then, for diffing 2 json's:

    diffj old.json new.json
    
    0 讨论(0)
  • 2021-02-19 14:41

    Possible workaround with NPM 5.2+ using the npx command.

    npx sails new test-project
    

    See this question for a similar use case and elegant solutions.

    0 讨论(0)
  • 2021-02-19 14:44

    You should use the npm bin command to get an absolute path to your current node bin directory.

    For example:

    ➤ lessc
    bash: lessc: command not found
    ➤ npm bin
    /home/brice/[...]/node_modules/.bin
    ➤ export PATH=$(npm bin):$PATH
    ➤ lessc --version
    lessc 1.7.3 (Less Compiler) [JavaScript]
    

    This avoids the problem of relative paths, especially if you're going to be using this in a build system that will invoke the command in subdirectories.

    0 讨论(0)
  • 2021-02-19 14:45

    A bit more robust is:

    export PATH=$(npm bin):$PATH
    

    You can either run it, add it to your shell profile, or create an alias like:

    alias snpm='export PATH=$(npm bin):$PATH'
    

    If you do go the alias route, be sure to use single quotes so it delays the execution of the variables!

    0 讨论(0)
  • 2021-02-19 14:50

    To use on the command line like sails generate foo you will need to install the npm module globally.

    npm install -g sails
    

    You could also use the path to the bin in the command if you don't want to install globally:

    ./node_modules/sails/bin/sails.js generate foo
    
    0 讨论(0)
  • 2021-02-19 14:51

    The official instructions for sails (https://github.com/balderdashy/sails) advises

    To install the latest stable release with the command-line tool:

    sudo npm -g install sails
    

    This installs globally and adds to a directory like /usr/local/bin that should be in your $PATH.

    But to answer the general question regarding the location of the binaries if you install locally, they should be placed in ./node_modules/.bin directory (so run ./node_modules/.bin/sails ...)

    0 讨论(0)
提交回复
热议问题