NodeJS 作为npm脚本调用时,有效脚本不工作

cgvd09ve  于 2023-05-06  发布在  Node.js
关注(0)|答案(1)|浏览(179)

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'
  ]
}

怎么修?先谢谢你了。

v09wglhw

v09wglhw1#

此问题似乎仅在Windows上存在。这是由于npm使用cmd作为默认的Windows shell,不支持命令替换。将script-shell = powershell添加到.npmrc文件可解决问题。
有关更多信息,请参阅此跨环境github问题:https://github.com/kentcdodds/cross-env/issues/192

相关问题