Node 应用程序中相同 npm 包的两个版本 [英] Two versions of same npm package in Node application

查看:52
本文介绍了Node 应用程序中相同 npm 包的两个版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 NodeJS 中开发一个 CLI 工具,它使用我们开发的另一个 NodeJs 包,它是一个 SDK.

I'm working on a CLI tool in NodeJS that uses another NodeJs package that we develop, which is an SDK.

问题是,我们刚刚发布了该 SDK 的 V2 版本,我们希望为 CLI 用户提供传统模式,以便他们可以使用我们 SDK 的第一个或第二个版本,如下所示:

The thing is, we just published a V2 version of that SDK, and we want to offer the CLI user a legacy mode, so they can use either the first or second version of our SDK, like so:

$ cli do-stuff
#execute sdk v2

$ LEGACY_MODE='on' cli do-stuff
#execute sdk v1

我的问题是我没有找到任何干净的方法来在我的 CLI 中使用相同依赖项的两个版本.我尝试使用 npm-install-version 包.它在我的本地环境中运行良好,但是在发布我的 cli 并执行 npm install -g my-cli 后,它不再起作用,因为它在当前文件夹中创建了一个 node_modules 文件夹,而不是/usr/local/lib/node_modules/my-cli 文件夹.我也试过 multidep,我也遇到了同样的问题.

My problem is that I did not found any clean way to use two versions of the same dependency in my CLI. I tried to use npm-install-version package. It works well on my local environment, but after publishing my cli and doing npm install -g my-cli, it doesn't work anymore, because it creates a node_modules folder in the current folder, instead of the /usr/local/lib/node_modules/my-cli folder. I also tried multidep, and I have kind of the same issue.

目前,我的 package.json 根本不包含我的 sdk,但我想要类似的东西:

For now, my package.json do not contain at all my sdk, but I would like to have something like :

"dependencies": {
  "my-sdk": "2.0.0"
  "my-sdk-legacy": "1.0.0"
}

"dependencies": {
  "my-sdk": ["2.0.0", "1.0.0"]
}

我还没有找到其他东西.我正在考虑使用另一个名称发布我的 sdk 包的第一个版本,例如my-sdk-legacy",但我想尽可能避免这种情况.

I haven't found anything else yet. I'm thinking about publishing the first version of my sdk package with another name, like "my-sdk-legacy", but I would like to avoid that if possible.

有什么解决办法吗?

推荐答案

所以这实际上是一个很常见的场景,已经多次解决.

So this is actually a quite common scenario which was addressed several times.

npm 有一个已解决的问题,yarn 包管理器.

There is a closed issue for npm and open issue for yarn package managers.

NPM 的作者在 this 中提出了第一个解决方案GH评论:

The first solution was suggested by the author of NPM in this GH comment:

以不同的名称发布一个单独的包.它将需要一个特定的版本.

Publish a separate package under a different name. It will require a specific version inside.

{ "name": "express3",
  "version": "1.0.0",
  "description":"Express version 3",
  "dependencies": { "express":"3" } }

// index.js
module.exports = require('express')

在您的情况下,您将发布 my-sdk-v1my-sdk-v2.从现在开始,您可以轻松地在一个项目中安装一个包的 2 个版本,而不会遇到冲突.

In your case you'll publish my-sdk-v1 and my-sdk-v2. And from now you can easily install 2 versions of a package in one project without running into conflicts.

const mySDKLegacy = require('my-sdk-v1');
const mySDKModern = require('my-sdk-v2');

第二种方法提出了几乎相同的想法 - 使用 git网址:

The second way pretty much the same idea proposed - use git url:

{
    "my-sdk-v1": "git://github.com/user/my-sdk#1.0.0",
    "my-sdk-v2": "git://github.com/user/my-sdk#2.0.0"
}

与 npm 包不同,您可以随意选择任何名称!真相的来源是 git url.

Unlike npm package, you are free to choose any name you wish! The source of truth is the git url.

稍后 npm-install-version 弹出.Buuut,正如您已经证明的那样,它的使用有点有限.因为它会产生一个子进程来执行一些命令并写入 tmp 目录.对于 CLI 来说,这不是最可靠的方法.

Later npm-install-version popped up. Buuut, as you already proved, its usage is a bit limited. Since it spawns a child process to execute some commands and writes to tmp dirs. Not the most reliable way for a CLI.

总而言之:您只剩下选择 1 &2. 我会坚持第一个,因为 github repo name &标签可能会改变.

To sum up: you're left with choices 1 & 2. I'd stick with the first one, since the github repo name & tags could change.

当您想更频繁地更改版本以依赖时,带有 git url 的第二个选项更好.假设您想为 my-sdk-v1 旧版发布一个安全补丁.引用一个 git url 然后将 my-sdk-v1.1 一次又一次地发布到 npm 会更容易.

2nd option with git url is better when you want to change a version to depend more frequently. Imagine you want to publish a security patch for my-sdk-v1 legacy. Will be easier to reference a git url then publish my-sdk-v1.1 to npm again and again.

这篇关于Node 应用程序中相同 npm 包的两个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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