首页 文章

sudo:npm:找不到命令

提问于
浏览
100

我'm trying to upgrade to the latest version of node. I' m按照http://davidwalsh.name/upgrade-nodejs的说明进行操作

但当我这样做时:

sudo npm install -g n

我收到错误:

sudo: npm: command not found

npm没有sudo . 当我做:

whereis node

我知道了:

node: /usr/bin/node /usr/lib/node /usr/bin/X11/node /usr/local/node

运行:

which npm

显示:

/usr/local/node/bin/npm

我在https://stackoverflow.com/a/5062718/1246159尝试了解决方案

但我仍然得到同样的错误 . 我还查看了/ etc / sudoers文件,相关的行是:

Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

这看起来很好 . 我怎样才能让NPM使用sudo命令?

22 回答

  • 60

    要使用sudo权限全局安装npm软件包, /usr/bin/npm 应该可用 . 如果某些其他目录中存在 npm ,请创建一个软链接,如:

    sudo ln -s /usr/local/bin/npm /usr/bin/npm
    

    它适用于Fedora 25,node8.0.0和npm5.0.0

  • 1

    对于debian安装节点后输入

    curl -k -O -L https://npmjs.org/install.sh    
    ln -s /usr/bin/nodejs /usr/bin/node  
    sh install.sh
    
  • 0

    只需重新安装 .

    在RHEL,CentOS和Fedora上

    sudo yum remove nodejs npm
    sudo dnf remove nodejs npm   [On Fedora 22+ versions]
    

    然后

    yum -y install nodejs npm
    dnf -y install nodejs npm   [On Fedora 22+ versions]
    

    简单!..节点和npm现在就像一个魅力!

  • -7

    对于CentOS用户,这对我有用:

    sudo yum install npm
    
  • 3

    npm文件应该在 /usr/local/bin/npm 中 . 如果不存在,请在website上再次安装node.js . 这适用于我的情况 .

  • 1

    我必须做

    sudo apt-get install npm
    

    这对我有用 .

  • 68

    对于MAC用户,以下步骤对我有用 .

    $ brew update
    $ brew uninstall node
    $ brew install node
    $ brew postinstall
    
  • 0

    我有同样的问题;这是修复它的命令:

    • sudo ln -s /usr/local/bin/node /usr/bin/node

    • sudo ln -s /usr/local/lib/node /usr/lib/node

    • sudo ln -s /usr/local/bin/npm /usr/bin/npm

    • sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf

  • 100

    警告(编辑)

    做一个 chmod 777 是一个真正的脏解决方案 . 首先尝试这些,一次一个,并在一个工作时停止:

    • $ sudo chmod -R 777 /usr/local/lib/node_modules/npm

    • $ sudo chmod -R 777 /usr/local/lib/node_modules

    • $ sudo chmod g+w /usr/local/lib

    • $ sudo chmod g+rwx /usr/local/lib


    $ brew postinstall node 是我遇到问题的唯一安装部分

    Permission denied - /usr/local/lib/node_modules/npm/.github
    

    所以我

    // !! READ EDIT ABOVE BEFORE RUNNING THIS CODE !!
    $ sudo chmod -R 777 /usr/local/lib
    $ brew postinstall node
    

    和中提琴,现在联系

    $ npm -v
    3.10.10
    

    Extra

    如果您在 lib 上使用 -R 777 ,我的建议是将嵌套文件和目录设置为默认设置:

    • $ find /usr/local/lib -type f -print -exec chmod 644 {} \;

    • $ find /usr/local/lib -type d -print -exec chmod 755 {} \;

    • $ chmod /usr/local/lib 755

  • 0

    我必须做以下事情:

  • 0

    如果使用nvm安装了node / npm,则必须先运行nvm环境配置文件,然后才能使用任一软件包 .

    这通常在〜/ .nvm / nvm.sh中找到 .

    要自动运行,请包括:

    source ~/.nvm/nvm.sh
    

    在您的用户的.bash_profile文件中

    如果您希望对该用户使用sudo,请确保包含-i参数以使sudo设置用户环境 . 例如

    sudo -iu jenkins npm install grunt-cli
    
  • 18

    我有同样的问题,原因是在安装节点时没有安装npm包管理器 . 这是由于以下错误引起的:在安装过程中有一个名为“自定义设置”的步骤,在这里您可以选择以下选项之一:1)Node.js运行时(默认选中此选项) . 2)npm包管理器3)在线文档快捷方式 . 4)添加到路径 . 如果你继续,因为它是npm包管理器将不会被安装,因此你会得到错误 .

    解决方案:获取这些选项后,选择npm包管理器 . 这对我有用 .

  • 41

    我在CentOS上也是这样 .

    which npm 给出:

    $ which npm
    
    /usr/local/bin/npm
    

    哪里

    $ sudo which npm
    
    which: no npm in (/sbin:/bin:/usr/sbin:/usr/bin)
    

    我对Node也一样 .

    所以它可能是root不检查 /usr/local/bin 任何二进制文件的问题 .

  • 0

    如果您已经下载了节点包并在 /opt 之类的某处提取,则可以在 /usr/local/bin 中创建符号链接 .

    /usr/local/bin/npm -> /opt/node-v4.6.0-linux-x64/bin/npm
    /usr/local/bin/node -> /opt/node-v4.6.0-linux-x64/bin/node
    
  • 4

    我解决了这个问题

    apt-get install npm2deb
    
  • 0

    Instructions for installing Node.js来自包管理器:

    • Arch Linux

    • 基于Debian和Ubuntu的Linux发行版

    • 企业Linux和Fedora

    • FreeBSD和OpenBSD

    • Gentoo

    • NetBSD

    • openSUSE和SLE

    • OSX

    • SmartOS和illumos

    • Void Linux

    • Windows

  • 1

    使用visudo附加npm二进制路径到sudo路径并编辑“secure_path”

    现在“sudo npm”有效

  • 6

    完全删除节点:

    brew uninstall --force node
    

    再次安装:

    brew install node;
    which node # => /usr/local/bin/node
    export NODE_PATH='/usr/local/lib/node_modules'
    
  • 8

    我在Homestead也有同样的问题并尝试了很多方法 . 我试过了

    sudo apt-get install nodejs

    我收到以下错误:

    The following packages have unmet dependencies:
     npm : Depends: nodejs but it is not going to be installed
           Depends: node-abbrev (>= 1.0.4) but it is not going to be installed
           Depends: node-ansi (>= 0.3.0-2) but it is not going to be installed
           Depends: node-ansi-color-table but it is not going to be installed
           Depends: node-archy but it is not going to be installed
           Depends: node-block-stream but it is not going to be installed
           Depends: node-fstream (>= 0.1.22) but it is not going to be installed
           Depends: node-fstream-ignore but it is not going to be installed
           Depends: node-github-url-from-git but it is not going to be installed
           Depends: node-glob (>= 3.1.21) but it is not going to be installed
           Depends: node-graceful-fs (>= 2.0.0) but it is not going to be installed
           Depends: node-inherits but it is not going to be installed
           Depends: node-ini (>= 1.1.0) but it is not going to be installed
           Depends: node-lockfile but it is not going to be installed
           Depends: node-lru-cache (>= 2.3.0) but it is not going to be installed
           Depends: node-minimatch (>= 0.2.11) but it is not going to be installed
           Depends: node-mkdirp (>= 0.3.3) but it is not going to be installed
           Depends: node-gyp (>= 0.10.9) but it is not going to be installed
           Depends: node-nopt (>= 3.0.1) but it is not going to be installed
           Depends: node-npmlog but it is not going to be installed
           Depends: node-once but it is not going to be installed
           Depends: node-osenv but it is not going to be installed
           Depends: node-read but it is not going to be installed
           Depends: node-read-package-json (>= 1.1.0) but it is not going to be installed
           Depends: node-request (>= 2.25.0) but it is not going to be installed
           Depends: node-retry but it is not going to be installed
           Depends: node-rimraf (>= 2.2.2) but it is not going to be installed
           Depends: node-semver (>= 2.1.0) but it is not going to be installed
           Depends: node-sha but it is not going to be installed
           Depends: node-slide but it is not going to be installed
           Depends: node-tar (>= 0.1.18) but it is not going to be installed
           Depends: node-underscore but it is not going to be installed
           Depends: node-which but it is not going to be installed
    E: Unable to correct problems, you have held broken packages.
    

    Finally I tried with

    sudo apt-get dist-upgrade

    它工作正常 .

    root@homestead:/usr/local/bin# npm -v
    3.10.10
    
    root@homestead:/usr/local/bin# node -v
    v6.13.0
    
  • 1

    My solution is:

    sudo -E env "PATH=$PATH" n stable
    

    对我来说很好 .

    在这里找到它:https://stackoverflow.com/a/29400598/861615

    发生这种情况是因为您更改了默认的全局包目录

  • 2

    如果对使用 rh-* 包的人有用,这对我有用:

    sudo ln -s /opt/rh/rh-nodejs8/root/usr/bin/npm /usr/local/bin/npm
    
  • 10

    安装node.js并简单运行

    npm install -g bower
    

    来自你的项目目录

相关问题