node.js 更改 npm 脚本的工作目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30286498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change working directory for npm scripts
提问by eljefedelrodeodeljefe
Q:Is it possible to change the the context in which npm runs scripts?
问:是否可以更改 npm 运行脚本的上下文?
What I want to is 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/ && ./configurewould work on UNIX-like systems but not on windows.
显然cd deps/2.7/cpython/ && ./configure可以在类 UNIX 系统上工作,但不能在 Windows 上工作。
Why:The root of the problem is, that the configurecommand of the python repo outputs files into the directory where it is called. The files however are build relevant for makeand make installwhich look for the files in the directory of the repo.
为什么:问题的根源在于,configurepython repo的命令将文件输出到调用它的目录中。然而,这些文件是构建相关的,make并且make install会在 repo 的目录中查找文件。
In this case I can't change the Makefilesince the build process of Python is understandably complex.
在这种情况下,我无法更改 ,Makefile因为 Python 的构建过程是可以理解的复杂。
Alternative:The alternative is probably to write some install.jsand use node's OS independent API and some child_process.exec(), which I am probably gonna do. However, not leaving npmwould be really nice.
替代方案:替代方案可能是编写一些install.js并使用节点的操作系统独立 API 和一些child_process.exec(),我可能会这样做。但是,不离开npm真的很好。
回答by eljefedelrodeodeljefe
npmallows only to do cd dir && command -args, which will also run on Windows.
npm只允许 do cd dir && command -args,这也将在 Windows 上运行。
A change to use node's spawn functionality has been made in PR https://github.com/npm/npm/pull/10958, but was rejected, due to the above solution.
nodePR https://github.com/npm/npm/pull/10958 中对 use的 spawn 功能进行了更改,但由于上述解决方案而被拒绝。
回答by Paul Sweatte
As noted above:
如上所述:
npm is probably using
npm 可能正在使用
var spawn = require('child_process').spawn
which would allow you to set options like:
这将允许您设置如下选项:
{cwd: pwd + 'somepath'}
but isn't exposing it.
I've solved it with a fairly large
install.js, which does roughly that and it gets called frompackage.jsonlike above. The API ofchild_processisn't that easy to handle, though, since it throws loads of hard to debug errors. Took me some time, but I am happy now.
但没有暴露它。
我已经用一个相当大的 解决了它
install.js,它大致可以做到这一点,并且从package.json上面调用它。但是,APIchild_process并不那么容易处理,因为它会引发大量难以调试的错误。花了我一些时间,但我现在很高兴。

