Why `npm update -g` is not updating my global packages to the latest version? - npm

I'm following steps from updating global packages, so I executed npm outdated -g --depth=0 and got:
Package Current Wanted Latest Location
typescript 2.2.2 2.2.2 2.4.1
Then, I executed npm update -g, but I still got the same output from npm outdated -g --depth=0.
Executing npm list -g --depth=0 also confirms that the typescript package has not been updated to 2.4.1:
+-- bower#1.8.0
+-- gulp#3.9.1
+-- typescript#2.2.2
`-- typings#2.1.1
What am I missing?

You will have to either use this script or do them one by one it seems.
This global update is a known breaking point.
Here is the reference to this issue. They seem to have closed it without addressing the issue

You can also install specific version:
npm install -g typescript#2.4.1

As of now, there is no one simple command that does this.
You can use the following steps instead to find outdated packages and update them one by one.
Determining which global packages need updating: To see which global packages need to be updated, on the command line, run:
npm outdated -g --depth=0
Updating a single global package: To update a single global package, on the command line, run:
npm update -g <package_name>

To automatically update all global packages to the 'Latest' version in a single command:
npx npm-check --global --update-all
That will update all global packages to the 'Latest' version. More information is available about npm-check, including the ability to perform an interactive update, exclude packages, etc.
Conversely, npm update -g only updates global packages to the 'Wanted' version shown by npm outdated --global, as globally installed packages are treated as if they are installed with a caret semver range specified.
Lastly, if you happen to want to update (install) a package to a version other than 'Latest' or 'Wanted':
npm install --global <pkg>#<version>

Related

How to update npm package's dependency?

I am trying to update dependency of package I am installing trough npm.
When I install appium package, I get version 1.22.0 which is correct. But this package also have dependencies that getting regular updates on github and are propagated on npmjs.
But when I install main package, dependency is not latest.
For example, I need to update appium-espresso-driver dependency of appium to 1.50.1, but everytime I install appium, dependency is only 1.45.3, even when appium have ^1.0.0 in package.json
How to update this? Do I need to wait for appium package to be bumped?
If you want to update all the packages to the latest version and you are using npm, you can see this documentation npm-update, but the short answer is:
npm update
This helps to update every package of the project, you can do it in the root folder, and update everything in the package.json.
If you want to update everything to a latest version, you can use npm-check-updates, this will check the latest version of the packages that you have installed on package.json, I will show you the easy steps to install this but you can go through the documentation of the module in here:
npm install -g npm-check-updates
This will install you the package, then after that you need to run in in the root folder:
ncu -u
So after that command it will update all the packages in the package.json but not install them, so after running ncu -u you need to run again:
npm install
To install the new versions of the package.

How does `npm outdated -g` actually work?

I have some globally installed packages and I want to update some of them. I checked with
$ npm outdated -g
Package Current Wanted Latest Location
eslint 5.16.0 5.16.0 6.8.0 global
jshint 2.10.2 2.11.0 2.11.0 global
n 2.1.12 2.1.12 6.2.0 global
npx 10.2.0 10.2.2 10.2.2 global
I tried
$ npm update -g eslint --dd
but I got the message
outdated not updating eslint because it's currently at the maximum version that matches its specified semver range
I checked the documentation with
$ npm help outdated
wanted is the maximum version of the package that satisfies the
semver range specified in package.json. If there's no available semver
range (i.e. you're running npm outdated --global, or the package isn't included in package.json), then wanted shows the currently-installed version.
But that's obviously not true because
$ npm update -g jshint
worked and the values of Current and Wanted for the package jshint were different before the update. How is the value of wanted actually defined?
Sidequestion: What is the npm-way to update all outdated packages and what is the npm-way to update one package?
EDIT:
To check if it's related to major and minor version numbers I installed
$ npm install -g n#6.1.0
$ npm install -g eslint#6.7.0
$ npm install -g generator-wombytes-cpp#0.2.0
and I updated the other packages. Now the output is
$ npm outdated -g
Package Current Wanted Latest Location
eslint 6.7.0 6.8.0 6.8.0 global
generator-wombytes-cpp 0.2.0 0.2.0 0.3.0 global
n 6.1.0 6.2.0 6.2.0 global
There is a different behavior for these packages.

npm5 equivalent to yarn's --pure-lockfile flag?

I'm looking for an equivalent for yarn's --pure-lockfile flag.
This flag is useful when installing dependencies in CI, when you want it to read your lockfile but not modify it.
Does npm v5 have an equivalent?
npm 5.7 introduced the npm ci subcommand:
the main differences between using npm install and npm ci are:
The project must have an existing package-lock.json or npm-shrinkwrap.json.
If dependencies in the package lock do not match those in package.json, npm ci will exit with an error, instead of updating the package lock.
npm ci can only install entire projects at a time: individual dependencies cannot be added with this command.
If a node_modules is already present, it will be automatically removed before npm ci begins its install.
It will never write to package.json or any of the package-locks: installs are essentially frozen.
this is how I did in my dockerfile
RUN npm install --pure-lockfile
it should work perfect.

How do I force npm to reinstall a single package, even if the version number is the same?

In my Node.js project, I have a dependency on another local project. Oftentimes, I need to make a small change to the dependency and see how it affects my main project. In order to do this, I have to reinstall my dependency using npm.
I can use npm update to try to update my dependency, but this seems like it will only work if the version number has changed on the dependency. I don't want to have to change the version number on my dependency every time I change a line of code or two to make an experimental change in development.
I can rm -rf node_modules/; npm install to ensure that I get the latest versions of all of my dependencies. Downloading all of my non-local dependencies takes several minutes, breaking up my train of thought.
Is there a way to force npm to reinstall a single dependency, even if that dependency's version number hasn't changed?
When you run npm install, it will install any missing dependencies, so you can combine it with an uninstall like this:
npm uninstall some_module; npm install
With npm 5, uninstalled modules are removed from the package.json, so you should use:
npm uninstall some_module; npm install some_module
On npm v 6.14:
npm install module_name --force --no-save
You get a message stating:
npm WARN using --force I sure hope you know what you are doing.
And then it proceeds to uninstall and reinstall the package.
Note: if you don't specify the --no-save option, npm updates the package version on package.json to the highest version that is compatible with the existing SemVer rule.
If you do not want npm to update the package's version on package.json, keep the --no-save option.
Not the best answer, but just for information, you can run
npm ci
It is the same as npm install, but it will remove the existing node_modules folder, if any, and do a fresh install for all packages. This is useful if the files in node_modules have been changed for some reason and you want to revert them to their original state.

What do the --save flags do with npm install

I see instructions to install a package with either
npm install <package_name>
or
npm install <package_name> --save
or
npm install <package_name> --save-dev
What is the difference between these options?
Updated, 2019:
Since this question was asked there was a change to npm, such that --save has become the default option, so you do not need to use --save to update the dependencies.
Original Answer:
npm install <package_name> --save installs the package and updates the dependencies in your package.json.
npm install <package_name> --no-save installs the package but does not update the dependencies as listed in your package.json.
npm install <package_name> ---save-dev updates the devDependencies in your package. These are only used for local testing and development.
You can read more at https://docs.npmjs.com/getting-started/using-a-package.json.
npm install takes 3 exclusive, optional flags which save or update the package version in your main package.json:
-S, --save: Package will appear in your dependencies.
-D, --save-dev: Package will appear in your devDependencies.
-O, --save-optional: Package will appear in your optionalDependencies.
When using any of the above options to save dependencies to your package.json, there is an additional, optional flag:
-E, --save-exact: Saved dependencies will be configured with an exact version rather than using npm's default semver range operator.
Further, if you have an npm-shrinkwrap.json then it will be updated as well.
<scope> is optional. The package will be downloaded from the registry associated with the specified scope. If no registry is associated with the given scope the default registry is assumed. See npm-scope.
Note: if you do not include the #-symbol on your scope name, npm will interpret this as a GitHub repository instead, see below. Scopes names must also be followed by a slash.
Examples:
npm install sax --save
npm install githubname/reponame
npm install #myorg/privatepackage
npm install node-tap --save-dev
npm install dtrace-provider --save-optional
npm install readable-stream --save --save-exact
Note: If there is a file or folder named <name> in the current working directory, then it will try to install that, and only try to fetch the package by name if it is not valid.
(from official docs) https://docs.npmjs.com/cli/install
The --save flag no longer serves a purpose.
Previously, as the other answers noted, the --save flag would update the dependencies in the project's package.json file, but npm install now includes this functionality by default.
At this point if you want to prevent npm install from saving dependencies, you have to use the --no-save flag.
Thanks to Coruscate5 for mentioning this in their comment.
More info in the npm-install documentation:
npm install saves any specified packages into dependencies by default. Additionally, you can control where and how they get saved with some additional flags:
-P, --save-prod: Package will appear in your dependencies. This is the default unless -D or -O are present.
-D, --save-dev: Package will appear in your devDependencies.
-O, --save-optional: Package will appear in your optionalDependencies.
--no-save: Prevents saving to dependencies.
When using any of the above options to save dependencies to your package.json, there are two additional, optional flags:
-E, --save-exact: Saved dependencies will be configured with an exact version rather than using npm’s default semver range operator.
-B, --save-bundle: Saved dependencies will also be added to your bundleDependencies list.