从不同的存储库运行 npm 脚本 [英] run npm script from different repository

查看:46
本文介绍了从不同的存储库运行 npm 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从存储库 B(均在本地克隆)中启动位于存储库 A 中的 npm run 脚本.

在存储库 A 中,脚本在 package.json 中定义如下:

脚本":{"my-script": "节点 my-script.js"}

... 它是通过 npm run my-script 启动的.以这种方式启动时它工作正常,没有错误.该脚本编译一些文件并将它们复制到 repo A 中的不同文件夹.

因此,我首先想到的是能够从存储库 B 运行该进程,只是从 npm 调用 node my-script.js.

脚本":{"my-script-in-other-repo": "节点 ../path/to/repo/my-script.js"}

... 并在存储库 B 中使用 npm run my-script-in-other-repo 调用它.

不幸的是,这种方法会引发一堆 Node 和 NPM 错误,这些错误无助于解决此问题.诸如确保你安装了最新版本的node.js和npm..这是输出.

我的直觉告诉我,这可能是 Node 上下文的问题,或者存储库 B 中的 Node 无法访问存储库 A 中的开发包..>

一个快速而肮脏的解决方案可能是在存储库 B 中复制 my-script.js 更改文件的路径并复制相同的依赖项......但它有点丑.

有没有更简洁的方法来解决这个问题?

解决方案

您似乎将您的项目 A 作为 B 的依赖项,并且您有脚本A 旨在作为独立应用程序直接执行.幸运的是,npm 中有一种轻松的方式来注册此类脚本并从依赖项运行它们.

在项目 A 中,使用 "bin" 属性package.json 来保存你的可执行脚本:

"bin": {"A-script": "my-script.js"},

在项目 B 中,将 A 作为依赖项(或 devDependency,取决于您何时需要),然后运行 ​​A-script就像是一个命令:

脚本":{"my-script-in-other-repo": "A-script"}

但是请注意,虽然模块解析在从外部工作目录运行脚本时仍然有效,但使用 fs 解析其他类型的资源取决于当前工作目录.根据 关于 fs 操作的文档:

<块引用>

可以使用文件名的相对路径.但是请记住,此路径将相对于 process.cwd().

如果您需要加载位于与源代码相关的某个位置的资源文件(例如 css 文件),请考虑使用 __dirname 全局变量以解析到该文件.这实际上可能是您问题的根源.

I want to launch an npm run script located in repository A from repository B (both cloned locally).

In repository A the script is defined like this in package.json:

"scripts": {
  "my-script": "node my-script.js"
}

... and it's launched with npm run my-script. It works fine when launched this way with no errors. The script compiles some files and copies them to different folders inside repo A.

So, my first thought to be able to run that process from repository B was to simply call node my-script.js from npm.

"scripts": {
  "my-script-in-other-repo": "node ../path/to/repo/my-script.js"
}

... and call it with npm run my-script-in-other-repo in repository B.

Unfortunately this approach throws a bunch of Node and NPM errors which do nothing to help solve this. Stuff like Make sure you have the latest version of node.js and npm installed.. Here is the output.

My intuition tells me it may be a problem with the Node context, or maybe Node in repository B can't access the dev packages in repository A.

A quick and dirty solution could be to replicate my-script.js in repository B changing the paths of the files and copying the same dependencies... but it is kinda ugly.

Is there a cleaner way to solve this?

解决方案

You seem to have your project A as a dependency of B, and that you have scripts in A that are intended to be executed directly as independent applications.Luckily, there is a painless way in npm to register such scripts and run them from dependents.

In project A, use the "bin" property in package.json to keep your executable scripts:

"bin": {
  "A-script": "my-script.js"
},

In project B, keep A as a dependency (or devDependency, depending on when you need it), then run A-script as if it were a command:

"scripts": {
  "my-script-in-other-repo": "A-script"
}

However note that, although module resolution still works when running scripts from a foreign working directory, resolving other kinds of resources using fs depends on the current working directory. According to the docs regarding fs operations:

The relative path to a filename can be used. Remember, however, that this path will be relative to process.cwd().

If you need to load a resource file (such as a css file) residing in some place relative to your source code, consider using the __dirname global variable in order to resolve to that file. This could have actually been the root of your problem.

这篇关于从不同的存储库运行 npm 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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