如何运行依赖包的 npm 脚本 [英] How do I run an npm script of a dependent package

查看:67
本文介绍了如何运行依赖包的 npm 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包,它本身在其 package.json 中有一个脚本,我希望能够在我的顶级项目中运行.这个包是我的顶级项目依赖项之一.我正在寻找一种直接或间接调用依赖包脚本的方法.

让我们假设我正在使用的模块名为 foo,我想运行的脚本是 updateFooData.

我尝试使用语法 npm run-script <package><...> 运行它,但这似乎是不推荐使用的功能,因为我在当前的 官方文档 但我在其他(非常旧的)搜索结果中看到它.

npm run-script foo updateFooData# npm 错误!缺少脚本:foo

我还研究了 npm api,虽然 npm.commands.run-script(args, callback) 会做我想做的事,但我不知道如何将模块加载到 npm

<代码>{...脚本":{"foo:updateFooData": "node --eval \"... ??;npm.commands.run-script('updateFooData', callback)\""}}npm 运行 foo:updateFooData# 显然失败

到目前为止,我发现唯一有效的是将 CD 放入子模块目录并从那里运行 npm.这不是我的首选解决方案.

cd node_modules/foonpm 运行 updateFooData

解决方案

我在尝试运行 geoip-lite 的 updatedb 脚本时遇到了这个问题.您应该使用 npm explore 命令,它会在依赖项的目录中生成一个新的 shell.>

因此,对于您的用例,请尝试 npm explore foo -- npm run updateFooData

I have a package that itself has a script in its package.json that I would like to be able to run in my top-level project. This package is one of my top-level projects dependencies. I'm looking for a way to directly or indirectly call the dependency packages script.

Let us assume the module name I'm working with is named foo and the script I want to run is updateFooData.

I tried using the syntax npm run-script <package> <...> to run it, but this appears to be deprecated functionality as I can't find it in the current official documentation but I see it in other (very old) search results.

npm run-script foo updateFooData

# npm ERR! missing script: foo

I also looked into the npm api and while npm.commands.run-script(args, callback) will do what I want, I can't figure out how to load the module into npm

{
  ...
  "scripts":{
    "foo:updateFooData": "node --eval \"... ??; npm.commands.run-script('updateFooData', callback)\""
  }
}

npm run foo:updateFooData
# Obviously fails

The only thing I've found that works so far is to CD into the submodule directory and run npm from there. This is not the preferred solution for me.

cd node_modules/foo
npm run updateFooData

解决方案

I ran into this trying to run the updatedb script for geoip-lite. You should use the npm explore command which will spawn a new shell in a dependencies' directory.

So for your use case, try npm explore foo -- npm run updateFooData

这篇关于如何运行依赖包的 npm 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆