Global Node modules not installing correctly. Command not found

前端 未结 9 995
北荒
北荒 2020-11-29 20:26

I am having a problem installing global node modules and everything I find online says the solve is just adding -g. Which is not the problem. I believe it\'s a linking issue

相关标签:
9条回答
  • 2020-11-29 20:33

    My npm couldn't find global packages as well. I did what Brad Parks suggested:

    npm config set prefix /usr/local
    

    Then I got a EACCES permissions error (DON'T USE sudo npm install -g <package>) and fixed it through the official npm docs: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

    1. On the command line, in your home directory, create a directory for global installations:
     mkdir ~/.npm-global
    
    1. Configure npm to use the new directory path:
     npm config set prefix '~/.npm-global'
    
    1. In your preferred text editor, open or create a ~/.profile file and add this line:
     export PATH=~/.npm-global/bin:$PATH
    
    1. On the command line, update your system variables:
     source ~/.profile
    
    1. Then install a package globally and test it! For example:
    npm install -g awsmobile-cli
    awsmobile configure
    
    
    0 讨论(0)
  • 2020-11-29 20:34

    Check your global Node module's binary folder, and add it to your $PATH.

    npm list -g | head -1
    

    If you use nodenv, the path will change whenever you install the global node version. Adding the path like this solves my problem.

    "$HOME/.nodenv/versions/$(nodenv global)/bin"
    

    Shortcut for adding the path to zsh

    $ echo 'export PATH="$HOME/.nodenv/versions/$(nodenv global)/bin"' >> ~/.zshrc
    
    0 讨论(0)
  • 2020-11-29 20:37

    I do not ever install any npm stuff, via sudo! I have my own reasons, but I just try to keep things simple, and user based, since this is a user development world, and not everyone has root access, and root/sudo installing things like this just seems to clutter up things to begin with. After all, all developers should be able to follow these instructions, not just privileged sudo users.

    This particular system is a RHEL7 accessed via SSH:

    Frequently one needs various versions of node, so I use NVM https://github.com/creationix/nvm

    So with that said, I can show you a working example for -g global installs, using NVM, NPM, and node paths not using root.

    set your prefix for .npm-packages if it isn't already. (note, thats a hyphen, not an underscore)

    nvm config ls
    prefix = "/home/<yourusername>/.npm-packages"
    

    Then adjust your ~/.bash_profile or .bashrc if you prefer readup on why and which here, with the following information.

    #PATH EXPORTS
    NODE_MODULES=$HOME/.npm                                          
    NPM_PACKAGES=$HOME/.npm-packages/bin                           
    export PATH=$PATH:$HOME/bin:$NODE_MODULES:$NPM_PACKAGES         
    
    #NVM ENABLE                                                 
    export NVM_DIR="$HOME/.nvm"                                   
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm 
    

    That pretty much covers all paths. For e.g., if you install gulp like this npm install -g gulp it symlinks in ~/.npm-packages/bin (note thats a hyphen, not an underscore). (no need for gulp-cli, or gulp-cl)

    You can pretty much replace/comment-out all other node path exports. You can put this path info below any other path info you already have, safely, without it overwriting that stuff.

    0 讨论(0)
  • 2020-11-29 20:39

    In my case, The NODE_PATH environment variable was empty. Check whether it is empty-

    echo $NODE_PATH
    

    if the NODE_PATH is empty. Then change ~/.bash_profile and add NODE_PATH

    nano ~/.bash_profile
    export NODE_PATH=`npm root -g`
    source ~/.bash_profile
    

    Now install npm modules again and check whether that is being installed on the path npm root -g

    0 讨论(0)
  • 2020-11-29 20:41

    Add /usr/local/share/npm/bin to your PATH (e.g., in .bashrc).

    For more info, see npm help npm:

    global mode: npm installs packages into the install prefix at prefix/lib/node_modules and bins are installed in prefix/bin.

    You can find the install prefix with npm get prefix or npm config list | grep prefix.

    0 讨论(0)
  • 2020-11-29 20:41

    This may mean your node install prefix isn't what you expect.

    You can set it like so:

    npm config set prefix /usr/local

    then try running npm install -g again, and it should work out. Worked for me on a mac, and the solution comes from this site:

    http://webbb.be/blog/command-not-found-node-npm/

    EDIT: Note that I just came across this again on a new Mac I'm setting up, and had to do the process detailed here on stackoverflow as well.

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