栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

以编程方式安装提供其版本的NPM软件包

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

以编程方式安装提供其版本的NPM软件包

NPM NodeJS API没有很好的文档说明,但是检查代码会有所帮助。

在这里,我们找到以下字符串:

install.usage = "npm install"   + "nnpm install <pkg>"   + "nnpm install <pkg>@<tag>"   + "nnpm install <pkg>@<version>"   + "nnpm install <pkg>@<version range>"   + "nnpm install <folder>"   + "nnpm install <tarball file>"   + "nnpm install <tarball url>"   + "nnpm install <git:// url>"   + "nnpm install <github username>/<github project>"   + "nnCan specify one or more: npm install ./foo.tgz bar@stable /some/folder"   + "nIf no argument is supplied and ./npm-shrinkwrap.json is "   + "npresent, installs dependencies specified in the shrinkwrap."   + "nOtherwise, installs dependencies from ./package.json."

我的问题是关于版本的,所以我们可以做:

hello-world@0.0.1
安装的
0.0.1
版本
hello-world

var npm = require("npm");npm.load({    loaded: false}, function (err) {  // catch errors  npm.commands.install(["hello-world@0.0.1"], function (er, data) {    // log the error or data  });  npm.on("log", function (message) {    // log the progress of the installation    console.log(message);  });});

我没有进行测试,但是我确信我们可以使用任何格式的

install.usage
解决方案。

我编写了一个函数,该函数将

dependencies
对象转换为可以传递给
install
函数调用的数组。

dependencies:

{   "hello-world": "0.0.1"}

该函数获取

package.json
文件的路径并返回字符串数组。

function createNpmDependenciesArray (packageFilePath) {    var p = require(packageFilePath);    if (!p.dependencies) return [];    var deps = [];    for (var mod in p.dependencies) {        deps.push(mod + "@" + p.dependencies[mod]);    }    return deps;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614055.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号