在Ubuntu上安装node-gyp出错

41
npm http 200 https://registry.npmjs.org/weak/-/weak-0.2.2.tgz
npm http GET https://registry.npmjs.org/bindings
npm http 304 https://registry.npmjs.org/bindings

> weak@0.2.2 install node_modules/weak
> node-gyp rebuild

Traceback (most recent call last):
  File "/usr/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py", line 18, in <module>
    sys.exit(gyp.script_main())
AttributeError: 'module' object has no attribute 'script_main'
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:337:16)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:789:12)
gyp ERR! System Linux 3.11.0-15-generic
gyp ERR! command "node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"

gyp ERR! node -v v0.10.15
gyp ERR! node-gyp -v v0.12.1
gyp ERR! not ok 
npm ERR! weak@0.2.2 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the weak@0.2.2 install script.
npm ERR! This is most likely a problem with the weak package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get their info via:
npm ERR!     npm owner ls weak
npm ERR! There is likely additional logging output above.

npm ERR! System Linux 3.11.0-15-generic
npm ERR! command "node" "/usr/bin/npm" "install" "weak@0.2.2"
npm ERR! node -v v0.10.15
npm ERR! npm -v 1.3.23
npm ERR! code ELIFECYCLE

npm ERR! not ok code 0

我没有直接依赖于weak或node-gyp,但我猜它们是我的其他依赖项(express、phantom、ejs、aws-sdk、moment)所要求的。有人遇到过这样的问题并且能够解决吗?

13个回答

35

4
应该被接受为正确答案。降级到2.6并使用PPA是最后的选择。 - Yauhen Yakimovich
当我运行 npm install 时,出现了这个错误。gip 包是 npm 的一个依赖项,因此删除它并不能解决我的问题,因为它也会删除 npm... 我使用的是 Ubuntu 14.04。 - pieroxy
1
对于那些遇到类似问题的人,这个解决方案可以在Ubuntu上为我解决以下错误。“error: no such option: --no-parallel” - Róisín Grannell

34
这个命令 sudo apt-get install build-essential 对我的情况有帮助。

2
在Ubuntu 18.04中,没有build-essentials,但在早期版本的Ubuntu中不存在这个问题。感谢您的支持。 - Aloy A Sen
Linux Mint 20.3 上的命令运行良好! - Arthur Ronconi
这个答案在我的Ubuntu 20上有效。 - Jaymoh
答案在Ubuntu 22.04上无效。 - Rareș Flueraș

24

这是有效的方法。在安装过程中需要使用Python 2.6。

#!/bin/bash
#On Ubuntu Saucy:
sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python2.6
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.6 20
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 10

#you can switch between 2.6 & 2.7 using:
sudo update-alternatives --config python

#Btw I installed node using ppa:chris-lea/node.js

https://github.com/TooTallNate/node-gyp/issues/363


sudo update-alternatives --config python 是什么意思?它会从2.6切换到2.7,然后再从2.7切换回2.6吗?我们如何知道我们当前正在使用哪个Python版本? - Hugolpz
你可以使用以下命令来评估每个命令行工具的版本: which $(readlink -f \which python`)` - test30
您可以使用命令 python --version 查看正在使用的 Python 版本。 - Pratik Mandrekar

21

就我个人而言,我在尝试在Ubuntu 14.04 (DigitalOcean)上安装Protractor时遇到了类似的问题。重新安装node-gyp解决了它:

apt-get install node-gyp

在我的Ubuntu 22上,Node 18可用。 - Capaj

10
在Ubuntu 18上,我不得不安装所需的构建库才能使其工作。
sudo apt-get install build-essential

在全新的Ubuntu 20安装中,我必须在安装“build-essential”之前运行“sudo apt update”。 - Erwol

7
以下是在Ubuntu系统上成功安装node-gyp的步骤:
1.首先,使用以下命令在Ubuntu中安装"make" 构建工具:
sudo apt-get update && \
sudo apt-get install build-essential software-properties-common -y;

2. 接下来您需要安装一个适当的C/C++编译器工具链。我们将使用以下命令安装GCC:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y && \
sudo apt-get update && \
sudo apt-get install gcc-snapshot -y && \
sudo apt-get update && \
sudo apt-get install gcc-6 g++-6 -y && \
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave 
/usr/bin/g++ g++ /usr/bin/g++-6 && \
sudo apt-get install gcc-4.8 g++-4.8 -y && \
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave 
/usr/bin/g++ g++ /usr/bin/g++-4.8;

3. 安装 Python 2.7 版本。(注意:node-gyp 不支持 Python 3)。

sudo apt update
sudo apt upgrade
sudo apt install python2.7 python-pip

4. 最后安装 node-gyp npm 包:

npm install -g node-gyp

附加但不重要: 如果您在使用node-gyp时遇到任何与原子键盘布局相关的问题,请安装以下一个包:

sudo apt-get install libxkbfile-dev

完成了!现在应该可以正常工作了。


4

我在尝试安装维基媒体扩展程序mathoid时,在Ubuntu 16.04上遇到了这个问题。

我尝试了所有建议的方法,但都没有成功,直到我做了以下操作:

sudo apt-get install librsvg2-2 librsvg2-dev

2
在Ubuntu 10.04上,安装libicu解决了我的问题。 sudo apt-get install libicu-dev

1
在Fedora 20上,重新安装gyp解决了这个问题。
sudo yum reinstall gyp

1

我已经安装了build-essential。我只需要运行:

sudo apt install python

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接