Npm install node gyp windows

Getting Node-gyp working on windows 10

I’m just learning nodejs on windows. It seems that the node-gyp package is incredibly painful to set up. I’ve tried many guides online but I feel like I am playing a guessing game. The windows version is quite fresh , only a week or so old.

( trying to take the least complicated path )

If the above didn’t work» go to https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules

If I just run node-gyp, I get

Cannot find module:

‘C:\Program Files\nodejs\node_modules\npm\node_modules\node_modules\node-gyp\bin\node-gyp.js’ ( etc ). Even though that file exists.

I’ve even tried uninstalling node , clearing out %appdata% cache etc and removing other things.

If I try to install somethin dependent on node-gyp , I get :

..\src\ursaNative.cc(157): warning C4244: ( etc ) ..

ERR! stack Error: msbuild failed with exit code: 1 gyp ERR! stack at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:258:23)

I have spent many , many hours on this — I cannot believe how painful this is.

  • Installed the VS Tools ( as above )
  • Installed the C++ Compiler
  • installed Build tools https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15
  • Tried setting the environment to VS2015 ( and VS 2017 )
  • tried to follow many stack overflow fixes and posts. Surely there is a sure path to get this going.
  • Various combinations of uninstalling / re-installing locally and globally.

Any ideas where to go from here?

5 Answers 5

your node-gyp module is initialised from the NPM but there is a fair chance that its correct path is not added to the environment variables in your system, Just add it to the system variables. Check the path variable if it has «C://Users/abc/AppData/Roaming/npm» if not, Please add it.

Also check if python is added to your environment variable.

Got to this post trying to get a correct set-up for node-gyp , after upgrading Node.js from v5.X to v12.X by overriding previous installation, and upgrading node-gyp .

It seems fairly easy to miss some step when you already have a Visual Studio environment and one or more versions of Python installed.

I will try to synthesise here the steps that worked for me.

CONTEXT

Below the details as per configuration:

Guideline

You might have already walked through some of the steps outlined here. If you incurred in some issues, you may be able to identify at which stage something went wrong by reviewing the steps in detail:

References

  1. node-gyp on GitHub: README.md (Generate Your Projects)
  2. A Comprehensive Guide to Fixing Node-Gyp Issues on Windows (by Joe Bustamante; March 27, 2019)
  3. Setting up None.js on Windows 10 (by Ferenc Hámori; May 17, 2016)
  4. Using Python on Windows (official documentation)

(1) Node.js

This guideline assumes that git is already installed in your machine (you will find the installer for Windows here anyways).

  • Install Node.js : download the last LTS version for your platform (Windows Installer .msi should work)

To verify it is correctly installed, in a test folder, add a file test.js with this javascript code: console.log(«Node is installed!»); , and in a terminal run: node test.js . You should be prompted with Node is installed! .

(2) npm

  • upgrade npm to the latest stable version using the npm-windows-upgrade package

Run as Administrator a PowerShell terminal:

You will be prompted to choose a version. The greatest available version among the options should be fine.

If you miss this package you might want to follow the instructions to install it by following the steps outlined in the GitHub repository, which basically are to type the following in a PowerShell console running as Administrator:

(3) Visual Studio

If you have Visual Studio already installed, you might skip this step, unless you wanted to also upgrade to the newest version.

Читайте также:  Canon g1010 драйвера linux

In some posts, you might have read that you can achieve this step by simply using the following command line, after installing node-gyp :

However, you could opt to achieve this by other means:

  1. you may have already an installation of Visual Studio and just want to configure node-gyp to use it
  2. you may prefer to do a separated installation of Visual Studio and later on configure node-gyp to use it

This is really up to you. At a later stage, in this guideline, we will walk through the steps to configure the node-gyp to use a specific Visual Studio version.

(4) Python

In the installation guideline for node-gyp (GitHub official repo) specifies which versions of Python are currently compatible with the latest node-gyp version on Unix and macOS. However it does not explain for Windows platforms (as at 1st of March 2020).

Although lack of documentation on this point, by having a look at other users’ issues with this, it is fair to assume that on Windows platforms, node-gyp is only supported for Python v2.7.X (reference).

  • download Python 2.7.X here: you can choose the bugfix release noted with an available link at the beginning.
  • do a normal installation, and take note of the folder (preferably in a common folder where you will have all the python versions)

(5) node-gyp

Now it is the moment to correctly set up your node-gyp configuration.

If you haven not installed it as yet:

(5.1) Set the Python version

According the documentation:

If the NODE_GYP_FORCE_PYTHON environment variable is set to the path of a Python executable, it will be used instead of any of the other configured or builtin Python search paths. If it’s not a compatible version, no further searching will be done.

  1. identify the full path to the C:\full_path\Python2.7.X\python.exe file (by full path we mean all: the folder path + the target file python.exe )
  2. go to Control Panel ->System and open Advanced System Settings, tab Advanced
  3. at the bottom, click the button Environmental Variables.
  4. a new panel pops up with two big sections: User Variables (only for the process owner), and System Variables (applicable to all the processes)
  5. on the System Variables, create a New entry
  6. name it NODE_GYP_FORCE_PYTHON , and as value use the full path to the python.exe file version 2.7.X , and click OK and you are done
    • for the variable to be available in the environment of your PowerShell terminal, you will need to close it and reopen a new terminal
  7. you have just fixed the python version to be used for node-gyp in your system

Alternatively you can use the command line below:

(5.2) Set the Visual Studio Build Tools version

This step should be fairly easy:

  1. identify the year of your Visual Studio edition (i.e. 2015, 2017, 2019)
  2. use it in the year part of the command line below:

For example, if you want it to use the MSBuild of 2019, use the command below:

That must have done it.

(6) Testing node-gyp by creating a simple add-on in C++

References

  1. Mastering Node.js: Build robust and scalable real-time server-side web (by Sandro Pasquali & Kevin Faaborg, 2017, Packt Publishing): adapted example from here
  2. C++ Addons (official documentation)
  3. V8 Embed (explanation on how V8 is embedded in C++ )
  4. V8::FunctinonCallbackInfo Class Template reference for Node.js v12.0

Hands on work

In a test folder, create test\hello_module subfolder with the following empty files:

  • hello_module\hello.cc (our source C++ native code)
  • hello_module\binding.gyp (instruction file for node-gyp )
  • hello_module\index.js (the wrapper)

In a terminal initialise the package by npm ini . You can choose the offered default value by just pressing Enter in all of them:

Now fill the files in with the contents specified below. Please, leave the index.js for the end, since we will be compiling before using it.

The hello.cc file content:

The binding.gyp file content:

You can leave the index.js file for the end.

Now, let’s build the project:

The test\hello_module\build folder should have been created with the basic project files to compile a C++ solution (this is what node-gyp basically aims to: that you can use any C++ compiler without having to use the GUI ; in this case Visual Studio ).

Читайте также:  Взлом вай фая через linux

Now, let’s build the addon:

At the end of both commands you should read gyp info ok to know that everything was okay (you might not see it on a PowerShell terminal because of the blue background; if so, you can edit the Properties of the window and change Screen Background to black).

This command should have created the test\hello_module\build\Release folder with the hello.node file.

Notes:

  1. If everything went ok , you are done in verifying your node-gyp installation: it works!
    • once you got this simple add-on to work, if you are still having issues with some package(s), that might be related to that package alone, or some of its dependencies, but your node or node-gyp configuration are okay
  2. if you encountered problems during the build, you might use the documentation to troubleshoot the source of the problem, or open a new post specifying which the errors you encountered are, and someone might be able to help

(7) Wrap and use the add-on in C++

This is an extra step. As you might have got here, why leaving it like this?

Now let’s write the wrapper hello_module\index.js file:

And in the test folder, create the test\hello_world.js file that uses our addon:

And in the terminal:

You should see Hello World! prompted on the screen.

Hope this helps anyone having issues to identify where exactly the configuration of node-gyp failed to meet requirements.

Atomic Spin

Atomic Object’s blog on everything we find fascinating.

A Comprehensive Guide to Fixing Node-Gyp Issues on Windows

node-gyp is a tool that enables the compilation of native add-on modules for Node in multiple platforms. It has widespread use and is included as a dependency in many NPM packages.

On most systems, this isn’t an issue, and installing node-gyp with the rest of your packages works as expected. Unfortunately, this is not the case with Windows, as is evidenced by this thread from 2015. The Windows environment makes getting node-gyp to work a less-than-stellar developer experience, full of multiple pitfalls and many ways for things to go wrong.

This guide is meant to help solve the issues that can arise when installing a package that requires node-gyp .

1. Try running npm install with the —no-optional flag.

If you’re lucky, the dependency that requires node-gyp will be optional, and you can skip the entire process required to get it working. Try running npm install –no-optional to install only the required dependencies.

If you run this and still get the error, I have bad news: You’re in for a bit of a ride. As we begin our journey into getting node-gyp up and running, here’s an important note for all of the steps that follow. Make sure that you’re always working in an elevated terminal (with administrator privileges) and that you restart your console whenever a download is complete.

2. Try downloading the windows-build-tools package.

According to the node-gyp documentation, this step should be the end-all-be-all solution to fixing node-gyp problems. For most people, that’s true. NPM has a package called windows-build-tools that should automatically install everything you need to get node-gyp working, including the Microsoft build tools, compilers, Python, and everything else required to build native Node modules on Windows.

The good news is that installing this package should take care of all of the wiring up of these components. The bad news is that there are a lot of things included in it.

Depending on the version you download, it can hover between three and eight gigabytes (all to get some dependencies installed!). The install can take upwards of 30 minutes depending on your connection, so don’t despair if it seems like the install is hanging for a while.

You can download them by running this command: npm install —global —production windows-build-tools —vs2015

Important note

If you run this command without any additional flags, you’ll install the files associated with the latest version of Visual Studio, which is VS2017 at the time of writing. However, node-gyp requires the v140 distributable, not the v150 (which comes with VS2017). This is why the —vs2015 flag is added to the end of the command, since that’s the last version of Visual Studio that came with the v140 package. You can see more notes about that near the bottom of the package’s website.

Читайте также:  New windows 10 xbox one

Hopefully, that’s all it will take for you to get everything installed. If you’ve still got issues, then you’re going to have to grab all of the required files manually.

3. Download the Visual Studio 2015 build tools manually.

Rather than installing the build tools through NPM, download them manually. You can find them on the Microsoft download page. Once they’re downloaded, just run the installer.

4. Tell Node to use the 2015 build tools.

Now that we have the build tools, we need to tell Node to use them. You’ll have to run this command: npm config set msvs_version 2015 –global

5. Make sure you have Python 2.7 installed.

Next up is to download Python 2.7. This is important–by default, you’ll install the newest version (3.x.x), but only 2.7 is compatible with node-gyp . If you try to use a newer version of Python, you’ll get a syntax error due to print being made into an actual function in Python 3.

If you have another version of Python already installed, that’s okay. They can coexist with each other. You can grab Python 2.7 at this link.

6. Set your Node config to use Python 2.7.

Now that you have Python 2.7, you’re going to have to set Node to use it by default. To do that, run this command: npm config set python python2.7

If you followed the last few steps, you should now have everything necessary to get node-gyp working. Make sure you’ve restarted your terminal and are running it as an administrator, and try doing your install again. Hopefully, at this point, you can successfully install the dependencies you need. If not, we’re going to have to try one last thing.

7. Repeat Step 2 with the Visual Studio 2017 build tools.

I’ve personally had issues when I’ve tried to download the 2017 version of the build tools, even when trying to use newer versions of node-gyp . If you look online, you’ll see lots of other people with the same problem, including some of the commenters on this StackOverflow question.

However, most of the documentation around node-gyp and compiling native Node modules on Windows doesn’t specify to use the —vs2015 flag, and some even mention downloading the 2017 version. If you’re still having issues with getting node-gyp to run, try repeating Step 2 while omitting the —vs2015 flag.

8. Try installing an older version of Node.

Still getting an installation error? Try installing an older version of Node. If you’re on an experimental version, try going back to the last stable release (and then make sure that you’re actually using that version when you try and do npm install ).

If that still doesn’t work, try going back to Node 8. You’ll find some issues online of people having issues with Node 10 that were resolved by downgrading to Node 8, and sometimes newer versions of Node don’t play nice with node-gyp .

9. File an issue.

If you’re still having trouble getting node-gyp to work, then your issue probably doesn’t fall into one of the more common problems. It’s worth doing some research online to see if that solves your problem. Otherwise, your best option is to file an issue on the GitHub page for node-gyp and see what advice you can get there.

Other Resources

Much of this information can now be found on the GitHub readme for node-gyp, but it wasn’t always there. It only made an appearance after a few thousand posts from disgruntled Windows users who just wanted to install some dependencies. Even then, the information is missing some of the issues that arise with installing the wrong version of the build tools.

Another great resource is Microsoft’s guide on working with Node on Windows. It even has sections dedicated to compiling native Node modules and resolving basic node-gyp problems.

Additionally, this question on StackOverflow contains useful answers and comments regarding this issue. Leave a note in the comments if you’ve found anything else helpful in solving node-gyp issues!

Оцените статью