NPM脚本-在哪里保存本地项目的自定义脚本?

时间:2019-08-23 11:52:11

标签: npm npm-scripts

我有一个角度项目,我试图在该项目中创建自定义脚本,这是我第一次使用脚本。我在执行脚本时遇到麻烦。我收到以下错误:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'patch' ]
2 info using npm@6.10.0
3 info using node@v10.10.0
4 verbose run-script [ 'prepatch', 'patch', 'postpatch' ]
5 info lifecycle myapp@4.0.211~prepatch: myapp@4.0.211
6 info lifecycle myapp@4.0.211~patch: myapp@4.0.211
7 verbose lifecycle myapp@4.0.211~patch: unsafe-perm in lifecycle true
8 verbose lifecycle myapp@4.0.211~patch: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/$
9 verbose lifecycle myapp@4.0.211~patch: CWD: /Users/core/www/myapp-client
10 silly lifecycle myapp@4.0.211~patch: Args: [ '-c', 'ngsw-patch.js' ]
11 info lifecycle myapp@4.0.211~patch: Failed to exec patch script
12 verbose stack Error: myapp@4.0.211 patch: `ngsw-patch.js`
12 verbose stack spawn ENOENT
12 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:48:18)
12 verbose stack     at ChildProcess.emit (events.js:182:13)
12 verbose stack     at maybeClose (internal/child_process.js:962:16)
12 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
13 verbose pkgid myapp@4.0.211
14 verbose cwd /Users/core/www/ourmgmt-client
15 verbose Darwin 18.7.0
16 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "patch"
17 verbose node v10.10.0
18 verbose npm  v6.10.0
19 error file sh
20 error code ELIFECYCLE
21 error errno ENOENT
22 error syscall spawn
23 error myapp@4.0.211 patch: `ngsw-patch.js`
23 error spawn ENOENT
24 error Failed at the myapp@4.0.211 patch script.
24 error This is probably not a problem with npm. There is likely additional logging output above.
25 verbose exit [ 1, true ]

到目前为止,我仅测试了一个简单的脚本,即:

console.log("Hello World");

因此,我猜测错误与脚本位置有关。 目前,我已将其保存在与package.json文件相同的位置。 这是正确的位置吗?我是否引用错误(见下文)? 如果不是,我打算在哪里存储自定义脚本?预先感谢。

这是我的package.json文件:

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "patch": "ngsw-patch.js"
  }

1 个答案:

答案 0 :(得分:1)

就像我在评论中解释的那样,最佳实践是将您的脚本放在“ scripts”文件夹下。

脚本无法运行的原因是因为您需要执行js。

NodeJS是可以实现此目的的工具。

所以您的脚本部分看起来像

"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"patch": "node ngsw-patch.js"

}

比简单地打电话

npm run patch 

或使用非常出色的工具“纱线”