npm@5 has been published, it has a new feature package-lock.json file (after npm install
) which confuses me. I want to know, what is the effect of this file?
相关问题
- Failed at the electron@1.8.2 postinstall script
- Webpack getting started, import error
- No member named ForceSet
- AWS Elastic BeanStalk nodejs Deployment error
- node local dependency installs as shortcut and nes
相关文章
- @angular-cli install fails with deprecated request
- Create React App not installing, showing an error
- new field false in Package.json
- npm : Postinstall not running in docker
- Babel CLI is extremely slow
- Docker and npm - gyp ERR! not ok
- Is there an npm module to modify a pdf file in nod
- Vscode: error TS2307: Cannot find module 'vsco
It's an very important improvement for npm: guarantee exact same version of every package.
How to make sure your project built with same packages in different environments in a different time? Let's say, you may use
^1.2.3
in yourpackage.json
, or some of your dependencies are using that way, but how can u ensure each timenpm install
will pick up same version in your dev machine and in the build server? package-lock.json will ensure that.npm install
will generate the lock file, when on build server or deployment server, donpm ci
(which will read from the lock file, and install the whole package tree)It stores an exact, versioned dependency tree rather than using starred versioning like package.json itself (e.g. 1.0.*). This means you can guarantee the dependencies for other developers or prod releases, etc. It also has a mechanism to lock the tree but generally will regenerate if package.json changes.
From the npm github pages:
Edit
To answer jrahhali's question below about just using the package.json with exact version numbers. Bear in mind that your package.json contains only your direct dependencies, not the dependencies of your dependencies (sometimes called nested dependencies). This means with the standard package.json you can't control the versions of those nested dependencies, referencing them directly or as peer dependencies won't help as you also don't control the version tolerance that your direct dependencies define for these nested dependencies.
Even if you lock down the versions of your direct dependencies you cannot 100% guarantee that your full dependency tree will be identical every time. Secondly you might want to allow non-breaking changes (based on semantic versioning) of your direct dependencies which gives you even less control of nested dependencies plus you again can't guarantee that your direct dependencies won't at some point break semantic versioning rules themselves.
The solution to all this is the lock file which as described above locks in the versions of the full dependency tree. This allows you to guarantee your dependency tree for other developers or for releases whilst still allowing testing of new dependency versions (direct or indirect) using your standard package.json.
NB. The previous shrink wrap json did pretty much the same thing but the lock file renames it so that it's function is clearer. If there's already a shrink wrap file in the project then this will be used instead of any lock file.