我有一个软件包,它本身在package.json
中有一个脚本,我希望能够在我的顶级项目中运行。这个包是我的顶级项目依赖项之一。我正在寻找一种直接或间接调用依赖包脚本的方法。
我们假设我使用的模块名称为foo
,我想要运行的脚本是updateFooData
。
我尝试使用语法npm run-script <package> <...>
来运行它,但这似乎是不推荐使用的功能,因为我无法在当前official documentation中找到它,但我在其他地方看到它(非常古老) )搜索结果。
npm run-script foo updateFooData
# npm ERR! missing script: foo
我也查看了npm api,虽然npm.commands.run-script(args, callback)
会做我想做的事,但我无法弄清楚如何将模块加载到npm
{
...
"scripts":{
"foo:updateFooData": "node --eval \"... ??; npm.commands.run-script('updateFooData', callback)\""
}
}
npm run foo:updateFooData
# Obviously fails
我发现到目前为止唯一可行的方法是将CD放入子模块目录并从那里运行npm。这不是我的首选解决方案。
cd node_modules/foo
npm run updateFooData
答案 0 :(得分:16)
我遇到了这个尝试运行geoip-lite的updatedb脚本。您应该使用npm explore command,它将在依赖项中生成一个新的shell&#39; 。目录
因此,对于您的使用案例,请尝试npm explore foo -- npm run updateFooData
答案 1 :(得分:0)
Note:
This isn't a very good idea. You have no guarantee which node_modules folder module will be installed in as NPM will attempt to optimise space by installing shared packages at the highest level possible. – @superluminary
Something I've found that does work:
If the script you are running runs a script file, you can look at the path of the file it's running and run the script using a require:
# if node_modules/foo/package.json looks like this
{
"scripts": {
"updateFooData":"scripts/updateFooData.js"
}
}
# then package.json can look like this
{
"scripts": {
"foo:updateFooData":"node --eval \"require('foo/scripts/updateFooData.js')\""
}
}
# or package.json can look like this
{
"scripts": {
"foo:updateFooData":"node node_modules/foo/scripts/updateFooData.js"
}
}
# and you can run it like this
npm run foo:updateFooData
I don't like this solution because it only works if the npm script you are running is a file. It won't apply in all