我有一个 super 项目,它托 pipe 项目并包含常见的依赖项:
super
node_modules
.bin
foo-binary
foo-binary.exe
foo
sub
node_modules
package.json
package.json
我需要能够调用foo
要么为 super
NPM 脚本:
super/package.json
"scripts": {
"foo": "foo-binary" <-- should run with super/sub/ as CWD
}
在本例中foo-binary
运行 super/
作为 CWD,而它应该以 super/sub/
运行。使用cd
导致跨平台问题; cd sub && ..\\node_modules\\.bin\\foo-binary
带有反斜杠的适用于 Windows,但不适用于 *nix 操作系统,而 cd sub && ../node_modules/.bin/foo-binary
使用正斜杠在 Windows 上失败(在 Windows 7 上测试):
".." is not recognized as an internal or external command, operable program or batch file
或者我需要能够调用foo
要么为 super
NPM 脚本:
super/sub/package.json
"scripts": {
"foo": "../node_modules/.bin/foo-binary"
}
在这种情况下,特定于平台的路径在 Windows 上也会失败。
sub
有几个原因不能有foo
作为其自身的依赖项,其中之一是所有子项目应一致使用相同的 foo
版本且不占用空间多个foo
副本。
在这种情况下如何设置当前工作目录,跨平台并且最好不向项目添加自定义脚本?
最佳答案
用 JSON 转义双引号将 npm 脚本中定义的路径括起来,即 \"...\"
。
例如:
"scripts": {
"foo": "cd sub && \"../node_modules/.bin/foo-binary\""
}
现在将通过 windows cmd.exe
和 *nix sh
成功跨平台运行。
关于node.js - 为运行二进制文件的 NPM 脚本设置 CWD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55664753/