npm 中文文档 npm 中文文档
指南
npmjs.com (opens new window)
指南
npmjs.com (opens new window)
  • 快速入门

    • npm 是什么?
    • npm 安装和更新
    • npm 防止权限错误
    • npm package.json 文件
    • npm 安装包
    • npm 更新包
    • npm 卸载包
    • npm 创建 Node.js 模块
    • npm 发布和更新包
    • npm 使用语义化版本
    • npm 使用 Dist-tags 标记包
    • npm 包和模块的了解
  • 命令行
  • 配置 npm

npm-lockdown


Put your dependencies on lockdown.

lockdown

What's this?


NPM Lockdown is a tool that locks your node.js app to specific versions of dependencies... So that you can:

know that the code you develop against is what you test and deploy
npm install and get the same code, every time.
not have to copy all of your dependencies into your project
not have to stand up a private npm repository to solve this problem.

Who is this for?


Node.JS application developers, but not library authors.  Stuff published in npm as libraries probably wouldn't be interested.

Why Care?


Even if you express verbatim versions in your package.json file, you're still vulnerable to your code breaking at any time.  This can happen if a dependency of a project you depend on with a specific version itselfdepends on another packages with a version range.

How can other people accidentally or intentionally break your node.js app? Well, they might...

... push a new version that no longer supports your preferred version of node.js.
... fix a subtle bug that you actually depend on.
... accidentally introduce a subtle bug.
... be having a bad day.

Usage!


  1. ``` sh
  2. npm install --save foo@0.8.1
  3. ./node_modules/.bin/lockdown-relock

  4. ```

npm-lockdown is easy to get started with.  It generates a single file that lists the versions and check-sums of the software you depend on, so any time something changes out from under you, npm install will fail and tell you what package has changed.

One Time Project Setup


npm install the version of lockdown you want: npm install --save lockdown
add a line to your package.json file: "scripts": { "preinstall": "lockdown" }
generate a lockdown.json: node_modules/.bin/lockdown-relock
commit: git add package.json lockdown.json && git commit -m "be safe"

Adding new modules


npm install the specific dependencies of your app npm install --save foo@0.8.1
re-generate your lockdown.json: node_modules/.bin/lockdown-relock
commit: git add package.json lockdown.json && git commit -m "be safe"

Changing dependencies once locked down


You update your dependencies explicitly, relock, and commit:

  1. ``` sh
  2. npm install --save foo@1.2.3
  3. node_modules/.bin/lockdown-relock
  4. git add lockdown.json package.json
  5. git commit -m "move to foo v1.2.3"

  6. ```

done!

Using an npm mirror


You can fetch resources from an npm mirror by specifying the NPM_CONFIG_REGISTRY environment variable when invoking npm install. If NPM_CONFIG_REGISTRY is not specified, http://registry.npmjs.org will be used.

  1. ``` sh
  2. NPM_CONFIG_REGISTRY=http://registry.npmjs.eu/ npm install

  3. ```

Notes:


You should use the latest stable version of lockdown, find it from the npm registry

Installing dependencies once locked down


  1. ``` sh
  2. npm install

  3. ```

Related Tools


yarn - Fast, reliable, and secure dependency management.

Fast:Yarn caches every package it downloads so it never needs to download the same package again. It also parallelizes operations to maximize resource utilization so install times are faster than ever.

Reliable:Using a detailed, concise lockfile format and a deterministic algorithm for installs, Yarn is able to guarantee that an install that worked on one system will work exactly the same way on any other system.

Secure:Yarn uses checksums to verify the integrity of every installed package before its code is executed.

npm shrinkwrap - NPM itself has a feature called "shrinkwrap" that

locks down the versions of a package's dependencies so that you can control exactly which versions of each dependency will be used when your package is installed.


At present (as of npm v1.1.33), the implementation of shrinkwrap has a couple flaws which make it unusable for certain applications:

No checksums!  NPM shrinkwrap does not guarantee bit-wise equality of the installed dependencies, so if an upstream server or author decides to change the contents of version 1.2.3 of foo, you'll install something different than you intended without knowing.
Does not play nice with optionalDependencies - If you "shrinkwrap" your app and you have an installed dep that is optional, the dependency is no longer optional.  This might not be what you want.

NOTE:you can combine lockdown with shrinkwrap just fine.  If all you care about is #1 above.

The path forward is to build checksums into shrinkwrap and kick lockdown to the curb, but until then, lockdown solves some problems.  (@izs is interested in patches ).

npm-seal - Solves the same problem as lockdown in a very different way.  Because seal is built to be used in concert with shrinkwrap, it suffers from the optionalDependencies issue described above.
Last Updated: 2023-05-15 10:22:02