脚本调用时有效脚本不起作用"/>
作为 npm 脚本调用时有效脚本不起作用
我有一个 Nest JS 应用程序。启动它时,我想将
package.json
的版本和最后的 git 提交日期作为环境变量传递:
cross-env VERSION=$(node -p "require('./package.json').version") LAST_COMMIT_DATE=$("git log -1 --format=%cd") NODE_ENV=development nest start --watch
此命令在 Windows 上按预期工作(在 Linux 上也应该如此)。然而,当我把它放入
start:dev
npm 脚本时:
"scripts": {
"start:dev": "cross-env VERSION=$(node -p \"require('./package.json').version\") LAST_COMMIT_DATE=$(\"git log -1 --format=%cd\") NODE_ENV=development nest start --watch"
}
并以
npm run start:dev
开始它会抛出一个奇怪的错误:
'-p' is not recognized as an internal or external command,
operable program or batch file.
Error: spawn -p ENOENT
at notFoundError (D:\Dev\tgr\tgrapi\node_modules\cross-spawn\lib\enoent.js:6:26)
at verifyENOENT (D:\Dev\tgr\tgrapi\node_modules\cross-spawn\lib\enoent.js:40:16)
at cp.emit (D:\Dev\tgr\tgrapi\node_modules\cross-spawn\lib\enoent.js:27:25)
at ChildProcess._handle.onexit (node:internal/child_process:291:12)
Emitted 'error' event on ChildProcess instance at:
at cp.emit (D:\Dev\tgr\tgrapi\node_modules\cross-spawn\lib\enoent.js:30:37)
at ChildProcess._handle.onexit (node:internal/child_process:291:12) {
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn -p',
path: '-p',
spawnargs: [
'require(./package.json).version)',
'LAST_COMMIT_DATE=$(git',
'log',
'-1',
'--format=%cd)',
'NODE_ENV=development',
'nest',
'start',
'--watch'
]
}
如何解决?提前致谢。
回答如下:问题似乎只出现在 Windows 上。这是由 npm 使用
cmd
作为默认 Windows shell 引起的,它不支持命令替换。将 script-shell = powershell
添加到 .npmrc
文件可解决问题。
有关更多信息,请参阅此跨环境 github 问题:https://github/kentcdodds/cross-env/issues/192
更多推荐
作为 npm 脚本调用时有效脚本不起作用
发布评论