由于https://github.com/npm/npm/issues/2943,npm永远不会支持别名包和安装同一包的多个版本的能力。
github问题上发布的变通方法可能适用于纯JS模块,但由于npm成为前端程序包管理的标准,所以程序包现在包含各种资产,如CSS。
是否有任何解决方法可以安装同一个软件包的多个版本?
我提出的最好的想法是“克隆”一个包,并以稍微不同的名称发布它。
例如,如果您需要jquery
的多个版本,则只需发布名为jquery-alias1
,jquery-alias2
,jquery-alias3
等的包,然后设置相应的版本你的package.json
。
或者您可以根据版本号命名包,例如jquery-1.11.x
,jquery-2.1.x
等。
这两种方法看起来都很草率。还有更好的吗?
答案 0 :(得分:47)
我想在这里发帖给像我这样使用Yarn并登陆的人。它或多或少是NPM的直接替代品,它支持开箱即用的混叠:
yarn add material-ui@latest
yarn add material-ui-next@npm:material-ui@next
then
import FlatButton from 'material-ui/FlatButton'; // v0.x
import Button from 'material-ui-next/Button'; // v1.x
(例如,信用证转到https://github.com/callemall/material-ui/issues/7195#issuecomment-314547601)
答案 1 :(得分:12)
自npm v6.9.0,起,npm现在支持程序包别名。 implements the same syntax是Yarn所使用的:
npm install jquery2@npm:jquery@2
npm install jquery3@npm:jquery@3
这会将以下内容添加到package.json
:
"dependencies": {
"jquery2": "npm:jquery@^2.2.4",
"jquery3": "npm:jquery@^3.4.1"
}
也可以使用这种语法直接从Github安装。例如,如果您要同时安装npm注册表版本和软件包foobar
的Github分支:
npm install foobar
npm install foobar-fork@github:username/foobar
答案 2 :(得分:1)
由于npm的工作原理,这很难干净利落,所以我会避免尝试在生产中这样做。
但是,对于集成测试和类似的用例,我创建了一个名为multidep的包,它允许您安装同一个包的多个版本,并require
,如下所示:
var multidepPackages = require('multidep')('test/multidep.json');
var jquery1 = multidepRequire('jquery', '1.11.3');
var jquery2 = multidepRequire('jquery', '2.1.4');
答案 3 :(得分:1)
就我而言,我需要安装比我已安装的版本更新的 react-table 7 版本,即全局 react-table 版本 6。所以我们面临着新开发的问题,我们需要使用新版本的表而不破坏应用程序中旧表的功能,所以我用不同的键安装了两个表-
例如
答案 4 :(得分:0)
NPM安装版本(https://github.com/scott113341/npm-install-version)也是一种选择。它基本上做了一些其他解决方案(技术上讲),但使用起来非常简单。安装有版本号的模块(NPM使用的标准@version命令参数)可以预先安装在具有该名称的 node_modules 下的子文件夹中。您还可以控制每个模块的目标目录 - 这对构建系统很有用。
GitHub文档中的使用代码段
const niv = require('npm-install-version');
const benchmark = require('./some-benchmark-function.js');
niv.install('csjs@1.0.0');
// installs csjs@1.0.0 to node_modules/csjs@1.0.0/
niv.install('csjs@1.0.1');
// installs csjs@1.0.1 to node_modules/csjs@1.0.1/
const csjs_old = niv.require('csjs@1.0.0');
const csjs_new = niv.require('csjs@1.0.1');
// require the old and new versions of csjs
benchmark([csjs_old, csjs_new], 'some-test-input');
// run our fake benchmark function on the old and new versions of csjs
答案 5 :(得分:0)
install-npm-version
(https://github.com/scott-lin/install-npm-version)是另一种选择。它可以在命令行上使用,也可以通过程序接口使用-用TypeScript编写的用于现代开发。
示例1:安装到版本化(默认)目录
import inv = require('install-npm-version');
inv.Install('chalk@2.4.0');
// installs chalk@2.4.0 to node_modules/chalk@2.4.0/
inv.Install('chalk@2.4.1');
// installs chalk@2.4.1 to node_modules/chalk@2.4.1/
示例2:安装到自定义目录
import inv = require('install-npm-version');
inv.Install('chalk@2.4.0', { 'Destination': 'some/path/chalk' });
// installs chalk@2.4.0 to node_modules/some/path/chalk/
示例#3:使用无声或嘈杂的标准输出进行安装
import inv = require('install-npm-version');
inv.Install('chalk@2.4.0', { 'Verbosity': 'Silent' });
inv.Install('chalk@2.4.0', { 'Verbosity': 'Debug' });
示例#4:覆盖现有安装
import inv = require('install-npm-version');
inv.Install('chalk@2.4.0', { 'Destination': 'mydir' });
// installs chalk@2.4.0 to node_modules/mydir/
inv.Install('chalk@2.4.1', { 'Destination': 'mydir' });
// does not install chalk@2.4.1 since node_modules/mydir/ already exists
inv.Install('chalk@2.4.1', { 'Destination': 'mydir', 'Overwrite': true });
// installs chalk@2.4.1 to node_modules/mydir/ by overwriting existing install
答案 6 :(得分:0)
在我的情况下,我需要安装一个比全局安装的版本更旧的create-react-app,因为我上的课程要求使用该较旧的版本进行作业。
我创建了一个仅包含此旧版本的新文件夹,将其cd入其中,然后执行了
npm init
设置此shell package.json后,我安装了所需的确切版本的create-react-app
npm install create-react-app@1.5.2
使用旧版本的create-react-app创建了一个本地node_modules文件夹。
然后,我创建了一个简单的bash脚本(create-react-app.sh)作为该旧版本的快捷方式,并使用了bash 变量“ $ @”以转发所有参数:
#!/bin/bash
{full-directory-path}/node_modules/create-react-app/index.js "$@"
最后,我使这个简单的bash脚本可执行
chmod u+x create-react-app.sh
因此直接运行此bash脚本将执行旧版本的create-react-app:
./create-react-app.sh --version
1.5.2