如果需要,npm检查并更新包

本文翻译自:npm check and update package if needed

We need to integrate Karma test runner into TeamCity and for that I'd like to give sys-engineers small script (powershell or whatever) that would: 我们需要将Karma测试运行器集成到TeamCity中,为此我想给sys-engineer小脚本(powershell或其他):

  1. pick up desired version number from some config file (I guess I can put it as a comment right in the karma.conf.js ) 从一些配置文件中获取所需的版本号(我想我可以把它作为注释放在karma.conf.js

  2. check if the defined version of karma runner installed in npm's global repo 检查是否在npm的全局仓库中安装了定义版本的karma runner

  3. if it's not, or the installed version is older than desired: pick up and install right version 如果不是,或者安装的版本比预期版本旧:选择并安装正确的版本

  4. run it: karma start .\\Scripts-Tests\\karma.conf.js --reporters teamcity --single-run 运行它: karma start .\\Scripts-Tests\\karma.conf.js --reporters teamcity --single-run

So my real question is: "how can one check in a script, if desired version of package installed?". 所以我真正的问题是:“如果安装了所需的软件包版本,如何检查脚本?”。 Should you do the check, or it's safe to just call npm -g install everytime? 您是否应该进行检查,或者每次只能调用npm -g install是安全的?

I don't want to always check and install the latest available version, because other config values may become incompatible 我不想总是检查并安装最新的可用版本,因为其他配置值可能会变得不兼容


#1楼

参考:https://stackoom.com/question/17L1C/如果需要-npm检查并更新包


#2楼

When installing npm packages (both globally or locally) you can define a specific version by using the @version syntax to define a version to be installed. 安装npm软件包(全局或本地)时,可以使用@version语法定义特定版本以定义要安装的版本。

In other words, doing: npm install -g karma@0.9.2 will ensure that only 0.9.2 is installed and won't reinstall if it already exists. 换句话说,执行: npm install -g karma@0.9.2将确保仅安装0.9.2并且如果已经存在则不会重新安装。

As a word of a advice, I would suggest avoiding global npm installs wherever you can. 作为建议的一句话,我建议尽可能避免全局npm安装。 Many people don't realize that if a dependency defines a bin file, it gets installed to ./node_modules/.bin/. 许多人没有意识到如果依赖项定义了一个bin文件,它就会被安装到./node_modules/.bin/。 Often, its very easy to use that local version of an installed module that is defined in your package.json. 通常,它非常容易使用package.json中定义的已安装模块的本地版本。 In fact, npm scripts will add the ./node_modules/.bin onto your path. 实际上,npm脚本会将./node_modules/.bin添加到您的路径中。

As an example, here is a package.json that, when I run npm install && npm test will install the version of karma defined in my package.json, and use that version of karma (installed at node_modules/.bin/karma) when running the test script: 作为一个例子,这是一个package.json,当我运行npm install && npm test将安装我的package.json中定义的karma版本,并使用该版本的karma(安装在node_modules / .bin / karma)运行test脚本:

{
 "name": "myApp",
 "main": "app.js",
 "scripts": {
   "test": "karma test/*",
 },
 "dependencies": {...},
 "devDependencies": {
   "karma": "0.9.2"
 }
}

This gives you the benefit of your package.json defining the version of karma to use and not having to keep that config globally on your CI box. 这为您提供了package.json的好处,它定义了要使用的karma版本,而不必在CI框中全局保留该配置。


#3楼

To check if any module in a project is 'old' you should do: 要检查项目中的任何模块是否“旧”,您应该:

npm outdated

'outdated' will check every module defined in package.json and see if there is a newer version in the NPM registry. 'outdated'将检查package.json中定义的每个模块,并查看NPM注册表中是否有更新的版本。

Here is an example, showing that xml2js (that is in node_modules/ in the current directory) is outdated, because a newer version exists (0.2.7): 下面是一个示例,显示xml2js(位于当前目录中的node_modules /中)已过时,因为存在较新的版本(0.2.7):

xml2js@0.2.7 node_modules/xml2js current=0.2.6

If you want to check for outdated modules and install newer version then you can do: npm update (for all modules) or npm update xml2js (only checks/updates xml2js) 如果你想检查过时的模块并安装更新的版本,你可以这样做: npm update (适用于所有模块)或npm update xml2js (仅检查/更新xml2js)

Have a look at the NPM docs: 看看NPM文档:


#4楼

npm outdated will identify packages that should be updated, and npm update <package name> can be used to update each package. npm outdated将识别应更新的软件包, npm update <package name>可用于更新每个软件包。 But npm update <package name> will not update the versions in your package.json which is an issue. 但是npm update <package name>不会更新package.json中的版本,这是一个问题。

The best workflow is to: 最好的工作流程是:

  1. Identify out of date packages 确定过时的包裹
  2. Update the versions in your package.json 更新package.json中的版本
  3. Run npm update to install the latest versions of each package 运行npm update以安装每个软件包的最新版本

Check out npm-check-updates to help with this workflow. 查看npm-check-updates以帮助完成此工作流程。

  • Install npm-check-updates 安装npm-check-updates
  • Run npm-check-updates to list what packages are out of date (basically the same thing as running npm outdated ) 运行npm-check-updates列出哪些软件包已过期(与运行npm outdated基本相同)
  • Run npm-check-updates -u to update all the versions in your package.json (this is the magic sauce) 运行npm-check-updates -u来更新package.json中的所有版本(这是魔术酱)
  • Run npm update as usual to install the new versions of your packages based on the updated package.json 像往常一样运行npm update ,根据更新的package.json安装新版本的软件包

#5楼

There is also a "fresh" module called npm-check : 还有一个名为npm-check的“新鲜”模块:

npm-check NPM-检查

Check for outdated, incorrect, and unused dependencies. 检查过时,不正确和未使用的依赖项。

在此输入图像描述

It also provides a convenient interactive way to update the dependencies. 它还提供了一种更新依赖关系的便捷交互方式。


#6楼

  • To update a single local package: 要更新单个本地包:

    1. First find out your outdated packages: 首先找出你过时的套餐:

      npm outdated

    2. Then update the package or packages that you want manually as: 然后手动更新您想要的软件包:

      npm update --save package_name

This way it is not necessary to update your local package.json file. 这样就没有必要更新本地的package.json文件。

Note that this will update your package to the latest version. 请注意,这会将您的包更新到最新版本。

  • If you write some version in your package.json file and do: 如果你在package.json文件中写了一些版本并执行:

    npm update package_name

    In this case you will get just the next stable version (wanted) regarding the version that you wrote in your package.json file. 在这种情况下,您将获得有关您在package.json文件中编写的版本的下一个稳定版本(通缉)。

And with npm list (package_name) you can find out the current version of your local packages. 使用npm list (package_name)您可以找到本地软件包的当前版本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值