【发布时间】:2020-12-04 18:37:41
【问题描述】:
我正在开发一个无服务器 NodeJS 应用程序,需要在离线模式下对其进行测试。我在package.json 文件中有一个npm 脚本,如下所示:
"scripts": {
"serve": "cross-env AUTHORIZER='{\\\"claims\\\":{\\\"permissions\\\":\\\"[view:accounts manage:accounts]\\\",\\\"sub\\\":\\\"auth0|5cfe0adce3c4c50ea072ea9f\\\"}}' AWS_PROFILE=elit_nonprd serverless offline start -s dev --noAuth",
...
请注意,有两个权限需要用空格分隔。在 Windows 上运行 npm run serve 会出现以下错误:
> @mypackage@1.0.0 serve C:\path
> cross-env AUTHORIZER='{\"claims\":{\"permissions\":\"[view:accounts manage:accounts]\",\"sub\":\"auth0|5cfe0adce3c4c50ea072ea9f\"}}' AWS_PROFILE=elit_nonprd serverless offline start -s dev --noAuth
The filename, directory name, or volume label syntax is incorrect.
events.js:288
throw er; // Unhandled 'error' event
^
Error: spawn manage:accounts]","sub":"auth0|5cfe0adce3c4c50ea072ea9f"}} ENOENT
at notFoundError (C:\path\node_modules\cross-spawn\lib\enoent.js:6:26)
at verifyENOENT (C:\path\node_modules\cross-spawn\lib\enoent.js:40:16)
at ChildProcess.cp.emit (C:\path\node_modules\cross-spawn\lib\enoent.js:27:25)
at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
Emitted 'error' event on ChildProcess instance at:
at ChildProcess.cp.emit (C:\path\node_modules\cross-spawn\lib\enoent.js:30:37)
at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12) {
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn manage:accounts]","sub":"auth0|5cfe0adce3c4c50ea072ea9f"}}',
path: 'manage:accounts]","sub":"auth0|5cfe0adce3c4c50ea072ea9f"}}',
spawnargs: [
'AWS_PROFILE=elit_nonprd',
'serverless',
'offline',
'start',
'-s',
'dev',
'--noAuth'
]
}
如果我将cross-env 替换为cross-env-shell,也会发生这种情况,尽管没有显示堆栈跟踪。
当值包含空格时,是否有一种通用的跨平台设置环境变量的方法?
更新:我希望的结果是将AUTHORIZER 设置为以下值(感谢@RobC 要求澄清):
{
"claims":
{
"permissions": "[view:accounts manage:accounts]",
"sub": "auth0|5cfe0adce3c4c50ea072ea9f"
}
}
【问题讨论】:
标签: node.js npm environment-variables npm-scripts