作为 npm 脚本调用时有效脚本不起作用

编程入门 行业动态 更新时间:2024-10-06 16:23:15

作为 npm <a href=https://www.elefans.com/category/jswz/34/1771291.html style=脚本调用时有效脚本不起作用"/>

作为 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 脚本调用时有效脚本不起作用

本文发布于:2024-05-30 15:49:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770661.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   不起作用   npm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!