Pass variable to node-gyp when running npm install

3k views Asked by At

I am using node-gyp to create a Node.js addon and my binding.gyp contains some variables as follows:

...
"link_settings": {
   "libraries": [
      "-lboost_program_options",
      "-lboost_log",
    ],
    "ldflags": [
       "-L<@(boost_root)/stage/lib",
       "-Wl,-rpath,<@(boost_root)/stage/lib",
     ]
  },
...

(complete gyp file from here ). I use node-gyp configure --boost_root=/PATH/TO/BOOST build to build the C++ sources. The problem arises when I run npm install since it just calls node-gyp rebuild without any parameters.

Is there any way to do any of the following?

  • Not run node-gyp rebuild when running npm install
  • Pass parameters to node-gyp when running npm install
2

There are 2 answers

0
Yan Foto On BEST ANSWER

After a lenghty discussion with @robertklep, I found out that passing the path as a command line flag also solves the problem:

npm install --boost_path=/PATH/TO/BOOST

however as he mentioned it is not known what happens if another package requires my package and tries to pass the parameter to it.

UPDATE:

As it turns out this solution can be applied also if another package uses original package:

npm link /PATH/TO/ORIGINAL_PACKET --boost_path=/PATH/TO/BOOST

or if you have installed from directly from the package manager and now want to install it through the depending package:

npm install --boost_path=/PATH/TO/BOOST
8
robertklep On

I've never tried myself, but according to this you can declare your own install script in package.json:

"scripts" : {
  "install" : "node-gyp configure --boost_root=/PATH/TO/BOOST build"
}

EDIT: just tried, works as advertised.

Instead of calling node-gyp from the install script, you can also call a shell script that you package as part of your module. For instance:

"scripts" : {
  "install" : "./scripts/build.sh"
}

The script would (somehow) determine the correct configuration settings and call node-gyp using them:

#!/bin/sh

...determine correct settings...

# call node-gyp
node-gyp configure --boost_root=$BOOST_PATH build

Make sure the script has executable permissions.

If you require the user to provide the path, the easiest would be to have them set an environment variable. Assuming it's called BOOST_PATH, this should work:

"scripts" : {
  "install" : "node-gyp configure --boost_root=\"$BOOST_PATH\" build"
}

The user could set it like so:

$ env BOOST_PATH=/PATH/TO/BOOST npm install yourmodule