npm install <module> persistent error ? (node-gyp build ?)

我们两清 提交于 2019-12-05 03:44:57

As you're finding out from the comments, this is a very common issue. So common, in fact, that the authors of jsdom have documented it right in the README file for the project's git repository.


TL;DR

You need to have a C++ compiler and Python2.7 installed on your machine to install contextify which is a dependency of jsdom. Otherwise, the jsdom install will fail.


From the README on the Github page for jsdom:

Contextify

Contextify is a dependency of jsdom, used for running <script> tags within the page. In other words, it allows jsdom, which is run in Node.js, to run strings of JavaScript in an isolated environment that pretends to be a browser environment instead of a server. You can see how this is an important feature.

Unfortunately, doing this kind of magic requires C++. And in Node.js, using C++ from JavaScript means using "native modules." Native modules are compiled at installation time so that they work precisely for your machine; that is, you don't download a contextify binary from npm, but instead build one locally after downloading the source from npm.

Unfortunately, getting C++ compiled within npm's installation system can be tricky, especially for Windows users. Thus, one of the most common problems with jsdom is trying to use it without the proper compilation tools installed. Here's what you need to compile Contextify, and thus to install jsdom:

Windows

  • A recent copy of the x86 version of Node.js for Windows, not the x64 version.
  • A copy of Visual C++ 2010 Express.
  • A copy of Python 2.7, installed in the default location of C:\Python27.

There are some slight modifications to this that can work; for example full versions of Visual Studio usually work, and sometimes you can even get an x64 version of Node.js working too. But it's tricky, so start with the basics!

Mac

  • XCode needs to be installed
  • "Command line tools for XCode" need to be installed
  • Launch XCode once to accept the license, etc. and ensure it's properly installed

Linux

You'll need various build tools installed, like make, Python 2.7, and a compiler toolchain. How to install these will be specific to your distro, if you don't already have them.

Try installing jsdom again after satisfying the requirements mentioned above for your OS, and see if that solves it.

By the way, you are getting the same issue with topojson because it has a dependency on d3 which in turn depends on jsdom, so it is just the same problem installing jsdom. Hope this helps!

--EDIT--

Since it sounds like you're using Ubuntu, I would recommend starting with the following command:

sudo apt-get install build-essential

This will install make and g++ and some other tools. This package and Python2.7 is probably about all you'll need to successfully install contextify.

Seems I found a way by carefully deleting all node-related file and folder on my computer.

0. Context: I previously made several unsuccessful console clean-ups, with

sudo apt-get remove --purge nodejs npm topojson

followed by ~3 different ways to reinstall nodejs. I tried EACH way, from clean-up to reinstall, between 2 & 4 times.

1. Terminal purging: Let's first cleaned up my mess the deepest way possible:

npm cache clean; bower cache clean; grunt clean;
sudo apt-get remove --purge nodejs npm topojson

Yet, I just noticed that this deep purge does NOT purge/delete installed node modules, which stay persistent.

2. Hand purge: Also, I pushed the purge further with an hand purge for both nodejs and node_modules. I looked for ALL related folders on my computer, nearer the root (ubuntu: /home/<user>/) the better. I was careful to delete all previously installed global .../node-modules/ folders, without deleting local modules (specific local installations, which were still working).

3. Reinstall: Then the following consoles allowed a clean reinstall:

sudo apt-get update
sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

sudo npm install -g topojson jsdom

Afterwhat, my file conversion via npm module topojson (depending itself on jsdom) worked fine :

cd /myfolder/
topojson -o output.topo.json input.geo.json   

4. Conclusion: I can not identify why, which file/folder deletion allowed the successful sudo apt-get install nodejs + sudo npm install -g topojson reinstall. But thist deep cleanup via mouse and recursive remove sudo rm -r [node related folder/file] definitively unlocked my situation.

/!\ Be very careful with the rm -r recursive remove. /!\

Solution 1b: I [suspect][1] the rm -r /home/<user>/local/lib/node_modules/ to be the key element unlocking the situation. You may list the global (-g) node_modules, then remove the relevant one(s) (topojson and/or jsdom and/or node-gyp) specifically, then reinstall it(them).

Commands: On ubuntu, try...

 cd /home/<user>/local/lib/node_modules/  #replace <user> by relevant user name to set your cwd
 ls -1      # give the list of global modules names
 rm -r /<module_name>    #replace <module_name> by relevant name to remove it
 sudo npm install -g <module_name>  #replace <module_name> by relevant name for global install

Note: topojson depends on jsdom and npm-gyp.

[1]: not tried, but makes sense according to my diagnostic.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!