Change working directory for npm scripts

Q: Is it possible to change the context in which npm runs scripts?

I want the following:

"scripts": { "test": "gulp mocha", "pre-install": "./deps/2.7/cpython/configure --prefix=$(pwd)/build --exec-prefix=$(pwd)/build && make -C deps/2.7/cpython && make -C deps/2.7/cpython install", "install": "node-gyp rebuild" }, 

Obviously, cd deps/2.7/cpython/ && ./configure will work on UNIX-like systems, but not windows.

Why: The root of the problem is that the python repo configure command dumps files to the directory in which it is called. Files, however, matter to make and make install , which look for files in the repo directory.

In this case, I cannot modify the Makefile , since the Python build process seems to be complicated.

Alternative: An alternative is perhaps to write install.js and use the node OS independent API and some child_process.exec() , which I'm probably going to do. However, not leaving npm would be very nice.

+42
npm makefile
May 17 '15 at 11:35
source share
2 answers

npm only allows cd dir && command -args , which will also work on Windows.

Changes in the use of the node spawn function were made in PR https://github.com/npm/npm/pull/10958 , but were rejected due to the above solution.

+42
Mar 04 '16 at 17:19
source share

As noted above:

npm probably uses

 var spawn = require('child_process').spawn 

which will allow you to set parameters such as:

  {cwd: pwd + 'somepath'} 

but does not expose it.

I solved it with a rather large install.js , which does something like this, and it is called from package.json as described above. The child_process API child_process not easy to handle, as it causes many hard debugging errors. Took me a while, but now I'm happy.

+3
Feb 17 '16 at 20:46
source share



All Articles