2014-10-16 50 views
22

由于https://github.com/npm/npm/issues/2943安装软件包的多个版本,故宫将永远支持能力,别名包和安装相同的包的多个版本。如何使用NPM

贴在GitHub的问题变通办法可能适用于纯JS模块,但NPM成为前端包管理的标准,现在包包括各种资产,如CSS。

是否有任何解决方法来安装相同包的多个版本?

我想出最好的办法就是“克隆”一个包,并用一个稍微不同的名字发布。

例如,如果你需要的jquery多个版本,你可以只发布jquery-alias2包称为jquery-alias1jquery-alias3等,然后在你的package.json设置相应的版本。

或者你可以根据自己的版本号命名该软件包,例如jquery-1.11.xjquery-2.1.x等。

这些方法似乎都马虎虽然。有更好的吗?

+0

是不是在鲍尔前端包管理的标准可以[很容易地做到这](http://stackoverflow.com/questions/16442012/亭子安装-2版本 - 的 - jQuery的)。 – laggingreflex 2014-10-17 01:55:14

+0

是的,凉亭看起来像是另一种选择。这似乎太麻烦了,似乎没有npm解决方案,因为向另一个大型团队推出另一个软件包管理系统可能很困难。特别是如果你已经有了基础设施来支持npm(例如,一个私人的npm注册服务器) – mark 2014-10-17 18:12:14

回答

4

听起来像“JSPM”可能正是您正在寻找的工具。 JSPM建立在NPM之上,但允许你从多个源(github,npm等)中提取包。它在前端使用System.js通用模块加载器来加载模块,并且“使用扁平版本管理下载到版本后缀文件夹”,这很容易推理。

jspm.io

当您安装使用JSPM您可以在包别名到一个特定的名称,包装以后可以require特别是在你的模块。

$ jspm install jquery 
... (status msgs) ... 
ok Installed jquery as github:components/[email protected]^2.1.4 (2.1.4) 

$ jspm install [email protected] 
... (status msgs) ... 
ok Installed jqueryOne as github:components/[email protected] (1.11.3) 

     github:components/jquery 1.11.3 2.1.4 

然后在你的JS,你可以简单地require(jquery)和/或require(jqueryOne)必要的,可以让你来回走是必要的。

对于任何想要使用多个版本的软件包,这都是一样的。

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'); 
14

我想这里发布像我这样的人使用Yarn并降落在这里。这是一个或多或少的直接替代的NPM,它支持混叠开箱:

yarn add [email protected] 
yarn add [email protected]:[email protected] 
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

+0

哇,正是我工作的包,非常感谢! – 2017-11-29 07:46:10

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('[email protected]'); 
// installs [email protected] to node_modules/[email protected]/ 

niv.install('[email protected]'); 
// installs [email protected] to node_modules/[email protected]/ 

const csjs_old = niv.require('[email protected]'); 
const csjs_new = niv.require('[email protected]'); 
// 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