- Npm Dependency Version Syntax Example
- Npm Install
- Semantic Versioning Npm
- Npm Semver Syntax
- Npm Check Version
How can we safely upgrade the npm dependencies in our project? What do the funny
^
and ~
characters mean in front of the dependency package versions? Gta san andreas for pc highly compressed. How can we do a major version upgrade on an npm dependency in our project? We’ll find out in this post.Version parts
- Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Reposted from Domenic's blog with permission. Npm is awesome as a package manager. In particular, it handles sub-dependencies very well: if my package depends on request version 2 and some-other-library, but some-other-library depends on request version 1, the resulting dependency graph looks like.
- Sep 24, 2017 Next in the sequence is module C which depends on again module B but with a different version. Npm handles this by nesting the new, different, module B version dependency.
npm package versioning follows semantic versioning. So, a package version has 3 parts - Major.Minor.Patch
For example, to specify acceptable version ranges up to 1.0.4, use the following syntax: Patch releases: 1.0 or 1.0.x or 1.0.4; Minor releases: 1 or 1.x or ^1.0.4; Major releases:. or x; For more information on semantic versioning syntax, see the npm semver calculator.
- Patch. This is incremented when a bug fix is made that won’t break consuming code
- Minor. This is incremented when features are added that won’t break consuming code
- Major. This is incremented when breaking changes are made
What does the ^
and ~
mean?
A version often has a
^
in front of it (e.g. ^16.8.6
). This means that the latest minor version can be safely installed. So in this example, ^16.12.1
can be safely installed if this was the newest version in 16.x
.Sometimes a version has a
~
in front of it (e.g. ~16.8.6
). This means that only the latest patch version can be safely installed. So in this example, ^16.8.12
can be safely installed if this was the newest version in 16.8.x
.So, npm install
installs the latest safe version of the dependencies?
Npm Dependency Version Syntax Example
Yes and no!
If the packages have already been installed into the
node_modules
folder, then npm install
won’t update any packages.If the packages haven’t been installed and a
package-lock.json
file exists, then npm install
will install the exact dependency versions specified in package-lock.json
.npm install
will install the latest safe version of the dependencies if they don’t exist in the node_modules
folder and, there is no package-lock.json
file. However, you may think the latest safe version hasn’t been installed because package.json
is unchanged, but if you check the packages in the node_modules
folder, the latest safe version will have been installed.So, how do I safely update all the dependencies?
Firstly, the dependencies that are out of date can be discovered by running the following command:
The dependencies will be listed out:
The wanted version is the latest safe version that can be taken (according to the semantic version and the
^
or ~
prefix). The latest version is the latest version available in the npm registry.All the dependencies can be safely updated to the wanted version by using the following command:
As well as updating the packages in the
node_modules
folder, the package.json
and package-lock.json
files will be updated.Npm Install
If we don’t want to update all of the packages, then the package names can be specified at the end of the command:
React is updated in the above example.
![Npm install Npm install](https://s3.amazonaws.com/codecademy-content/programs/react/creating-a-react-app/react_package_json.png)
Updating all dependencies with major changes
So, how do we upgrade dependencies when there has been a major version change? Pc clone ex lite keygen download. Spss license cost.
Perhaps the safest way is as follows:
- Check the changelog of the dependent package for breaking changes that could affect our app
- If we think we are safe to do the upgrade, run the following command:
- If multiple packages go together, you can list them all out. The example below will update React to the latest version:
Semantic Versioning Npm
- Verify the app isn’t broken by doing some tests
- Repeat the process for other packages where there is a major version change
Npm Semver Syntax
Is there a quicker way of just updating all the dependencies, including major version changes? So, like
npm update
but for major version updates as well?Yes, there is a tool called npm-check-updates that will do this. Just run the following command:
This will update the dependencies to the latest versions (including major version changes) in the
package.json
file. If we are happy to go ahead with the upgrades we need to run the following command:This will then upgrade the packages in the
node_modules
folder, and the package-lock.json
file will be updated as well.Npm Check Version
Wrap up
![Npm semver syntax Npm semver syntax](https://static.npmjs.com/338e4905a2684ca96e08c7780fc68412.png)
- Use
npm outdated
to discover dependencies that are out of date - Use
npm update
to perform safe dependency upgrades - Use
npm install <packagename>@latest
to upgrade to the latest major version of a package - Use
npx npm-check-updates -u
andnpm install
to upgrade all dependencies to their latest major versions