Node pre gyp install windows
node-gyp — Node.js native addon build tool
node-gyp is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. It contains a vendored copy of the gyp-next project that was previously used by the Chromium team, extended to support the development of Node.js native addons.
Note that node-gyp is not used to build Node.js itself.
Multiple target versions of Node.js are supported (i.e. 0.8 , . 4 , 5 , 6 , etc.), regardless of what version of Node.js is actually installed on your system ( node-gyp downloads the necessary development files or headers for the target version).
- The same build commands work on any of the supported platforms
- Supports the targeting of different versions of Node.js
You can install node-gyp using npm :
Depending on your operating system, you will need to install:
- Python v3.6, v3.7, v3.8, or v3.9
- make
- A proper C/C++ compiler toolchain, like GCC
ATTENTION: If your Mac has been upgraded to macOS Catalina (10.15), please read macOS_Catalina.md.
- Python v3.6, v3.7, v3.8, or v3.9
- Xcode
- You also need to install the XCode Command Line Tools by running xcode-select —install . Alternatively, if you already have the full Xcode installed, you can find them under the menu Xcode -> Open Developer Tool -> More Developer Tools. . This step will install clang , clang++ , and make .
Install the current version of Python from the Microsoft Store package.
Install tools and configuration manually:
- Install Visual C++ Build Environment: Visual Studio Build Tools (using «Visual C++ build tools» workload) or Visual Studio 2017 Community (using the «Desktop development with C++» workload)
- Launch cmd, npm config set msvs_version 2017
If the above steps didn’t work for you, please visit Microsoft’s Node.js Guidelines for Windows for additional tips.
To target native ARM64 Node.js on Windows 10 on ARM, add the components «Visual C++ compilers and libraries for ARM64» and «Visual C++ ATL for ARM64».
Configuring Python Dependency
node-gyp requires that you have installed a compatible version of Python, one of: v3.6, v3.7, v3.8, or v3.9. If you have multiple Python versions installed, you can identify which Python version node-gyp should use in one of the following ways:
- by setting the —python command-line option, e.g.:
- If node-gyp is called by way of npm , and you have multiple versions of Python installed, then you can set npm ‘s ‘python’ config key to the appropriate value:
If the PYTHON environment variable is set to the path of a Python executable, then that version will be used, if it is a compatible version.
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.
To compile your native addon, first go to its root directory:
The next step is to generate the appropriate project build files for the current platform. Use configure for that:
Auto-detection fails for Visual C++ Build Tools 2015, so —msvs_version=2015 needs to be added (not needed when run by npm as configured above):
Note: The configure step looks for a binding.gyp file in the current directory to process. See below for instructions on creating a binding.gyp file.
Now you will have either a Makefile (on Unix platforms) or a vcxproj file (on Windows) in the build/ directory. Next, invoke the build command:
Now you have your compiled .node bindings file! The compiled bindings end up in build/Debug/ or build/Release/ , depending on the build mode. At this point, you can require the .node file with Node.js and run your tests!
Note: To create a Debug build of the bindings file, pass the —debug (or -d ) switch when running either the configure , build or rebuild commands.
The binding.gyp file
A binding.gyp file describes the configuration to build your module, in a JSON-like format. This file gets placed in the root of your package, alongside package.json .
A barebones gyp file appropriate for building a Node.js addon could look like:
Some additional resources for Node.js native addons and writing gyp configuration files:
node-gyp responds to the following commands:
Command | Description |
---|---|
help | Shows the help dialog |
build | Invokes make / msbuild.exe and builds the native addon |
clean | Removes the build directory if it exists |
configure | Generates project build files for the current platform |
rebuild | Runs clean , configure and build all in a row |
install | Installs Node.js header files for the given version |
list | Lists the currently installed Node.js header versions |
remove | Removes the Node.js header files for the given version |
node-gyp accepts the following command options:
Command | Description |
---|---|
-j n , —jobs n | Run make in parallel. The value max will use all available CPU cores |
—target=v6.2.1 | Node.js version to build for (default is process.version ) |
—silly , —loglevel=silly | Log all progress to console |
—verbose , —loglevel=verbose | Log most progress to console |
—silent , —loglevel=silent | Don’t log anything to console |
debug , —debug | Make Debug build (default is Release ) |
—release , —no-debug | Make Release build |
-C $dir , —directory=$dir | Run command in different directory |
—make=$make | Override make command (e.g. gmake ) |
—thin=yes | Enable thin static libraries |
—arch=$arch | Set target architecture (e.g. ia32) |
—tarball=$path | Get headers from a local tarball |
—devdir=$path | SDK download directory (default is OS cache directory) |
—ensure | Don’t reinstall headers if already present |
—dist-url=$url | Download header tarball from custom URL |
—proxy=$url | Set HTTP(S) proxy for downloading header tarball |
—noproxy=$urls | Set urls to ignore proxies when downloading header tarball |
—cafile=$cafile | Override default CA chain (to download tarball) |
—nodedir=$path | Set the path to the node source code |
—python=$path | Set path to the Python binary |
—msvs_version=$version | Set Visual Studio version (Windows only) |
—solution=$solution | Set Visual Studio Solution version (Windows only) |
Use the form npm_config_OPTION_NAME for any of the command options listed above (dashes in option names should be replaced by underscores).
For example, to set devdir equal to /tmp/.gyp , you would:
Run this on Unix:
Or this on Windows:
Use the form OPTION_NAME for any of the command options listed above.
For example, to set devdir equal to /tmp/.gyp , you would run:
Note: Configuration set via npm will only be used when node-gyp is run via npm , not when node-gyp is run directly.
node-gyp is available under the MIT license. See the LICENSE file for details.
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
- node-gyp on GitHub: README.md (Generate Your Projects)
- A Comprehensive Guide to Fixing Node-Gyp Issues on Windows (by Joe Bustamante; March 27, 2019)
- Setting up None.js on Windows 10 (by Ferenc Hámori; May 17, 2016)
- 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.
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:
- you may have already an installation of Visual Studio and just want to configure node-gyp to use it
- 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.
- 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 )
- go to Control Panel ->System and open Advanced System Settings, tab Advanced
- at the bottom, click the button Environmental Variables.
- a new panel pops up with two big sections: User Variables (only for the process owner), and System Variables (applicable to all the processes)
- on the System Variables, create a New entry
- 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
- 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:
- identify the year of your Visual Studio edition (i.e. 2015, 2017, 2019)
- 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
- Mastering Node.js: Build robust and scalable real-time server-side web (by Sandro Pasquali & Kevin Faaborg, 2017, Packt Publishing): adapted example from here
- C++ Addons (official documentation)
- V8 Embed (explanation on how V8 is embedded in C++ )
- 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 ).
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:
- 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
- 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.