无法从 package.json 读取参数
Unable to read parameters from package.json
我无法从 package.json 读取参数,所以它总是 $(directory)。
这是我的命令:
npm run migrate -- -directory "migration_dir"
package.json
{
"name": "XXXX",
"version": "0.1.0",
"description": "XXX",
"main": "app.js",
"scripts": {
"migrate": "cd $(directory)"
},
"keywords": [
"XXXX"
],
"author": "XXXX",
"license": "MIT"
}
谢谢。
您可能面临两个问题。
1。 运行-script
的参数语法
来自 npm docs:
As of npm@2.0.0, you can use custom arguments when executing scripts. The special option --
is used by getopt to delimit the end of the options. npm will pass all the arguments after the --
directly to your script:
npm run test -- --grep="pattern"
--
之后的参数将直接 传递,在您的情况下是
$ cd $(directory) -directory "migration_dir"
要解决这个问题,您必须相应地更改脚本定义:
"scripts": {
"migrate": "cd"
}
并传递没有 -directory
选项的目录:
$ npm run migrate -- "migration_dir"
这将执行
$ cd "migration_dir"
2。脚本在 subshell
中执行
您发布的 migrate
脚本没有用,因为所有 npm 脚本都在 subshells 中执行,并且从 subshell 内部更改工作目录不会产生subshell 退出时的区别。
虽然如果您的脚本执行任何超出 cd
的操作,那将有效:
"scripts": {
"foo": "cd / && pwd"
}
&&
之后的部分脚本会看到cd
的效果,但是你的shell不会。
$ npm run foo
> cd / && pwd
/
$ pwd
/your/original/path
但是,您不能再使用 --
指定其他参数(npm run foo -- arg
可以 cd / && pwd arg
)。
看起来您想读取一个 ENV
参数。 Bash 表示法是 ${directory}
而不是 $(directory)
.
现在您可以运行迁移
directory="migration_dir" npm run migrate
输出为
cd migration_dir
此解决方案的优点是可以使用复杂的脚本。
我无法从 package.json 读取参数,所以它总是 $(directory)。 这是我的命令:
npm run migrate -- -directory "migration_dir"
package.json
{
"name": "XXXX",
"version": "0.1.0",
"description": "XXX",
"main": "app.js",
"scripts": {
"migrate": "cd $(directory)"
},
"keywords": [
"XXXX"
],
"author": "XXXX",
"license": "MIT"
}
谢谢。
您可能面临两个问题。
1。 运行-script
的参数语法来自 npm docs:
As of npm@2.0.0, you can use custom arguments when executing scripts. The special option
--
is used by getopt to delimit the end of the options. npm will pass all the arguments after the--
directly to your script:npm run test -- --grep="pattern"
--
之后的参数将直接 传递,在您的情况下是
$ cd $(directory) -directory "migration_dir"
要解决这个问题,您必须相应地更改脚本定义:
"scripts": {
"migrate": "cd"
}
并传递没有 -directory
选项的目录:
$ npm run migrate -- "migration_dir"
这将执行
$ cd "migration_dir"
2。脚本在 subshell
中执行您发布的 migrate
脚本没有用,因为所有 npm 脚本都在 subshells 中执行,并且从 subshell 内部更改工作目录不会产生subshell 退出时的区别。
虽然如果您的脚本执行任何超出 cd
的操作,那将有效:
"scripts": {
"foo": "cd / && pwd"
}
&&
之后的部分脚本会看到cd
的效果,但是你的shell不会。
$ npm run foo
> cd / && pwd
/
$ pwd
/your/original/path
但是,您不能再使用 --
指定其他参数(npm run foo -- arg
可以 cd / && pwd arg
)。
看起来您想读取一个 ENV
参数。 Bash 表示法是 ${directory}
而不是 $(directory)
.
现在您可以运行迁移
directory="migration_dir" npm run migrate
输出为
cd migration_dir
此解决方案的优点是可以使用复杂的脚本。