How does `npm outdated -g` actually work? - npm

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.

Related

How to Update Angular Cli

Just to give you some context.
I was running a project and after ng serve I was getting this warning message:
our global Angular CLI version (9.1.4) is greater than your local
version (1.0.0). The local Angular CLI version is used.
I googled and figured out I should update angular cli with the following command.
npm install --save -dev #angular/cli#latest
After that I am getting the following error when trying to build the project:
The build command requires to be run in an Angular project, but a project definition could not be found
Any help will be much appreciated.
After reading some issues reported on the GitHub repository, I found the solution.
In order to update the angular-cli package installed globally in your system, you need to run:
npm uninstall -g angular-cli
npm install -g #angular/cli#latest
Depending on your system, you may need to prefix the above commands with sudo.
Also, most likely you want to also update your local project version, because inside your project directory it will be selected with higher priority than the global one:
rm -rf node_modules
npm uninstall --save-dev angular-cli
npm install --save-dev #angular/cli#latest
npm install
After updating your CLI, you probably want to update your angular version too
Note: if you are updating to Angular CLI 6+ from an older version, you might need to read this
Edit: In addition, if you were still on a 1.x version of the cli, you need to convert your angular-cli.json to angular.json, which you can do with the following command:
ng update #angular/cli --from=1.7.4 --migrate-only
check here for more details.

Does npm track package compatibility?

If I have an old version of node (for example downloaded from Ubuntu repos), and I use sudo npm install #angular/cli -g, will that download the last version of #angular/cli that is known to work with that version of node, or will that always install the latest version of #angular/cli, even if that version is incompatible with my version of node?
I'm asking because I encountered a problem that makes me think that the second variant is what takes place. But at the same time, my experience with package managers like apt-get has led me to believe that they generally make sure to install compatible versions - apt-get will not install packages for Ubuntu 17.10.1 if your distro is Ubuntu 16.04.3 LTS, for example.
So I'm curious to know if npm behaves more like apt-get in that regard, or not.
npm does not behave like apt get in that regard, if you run npm install -g <package>, npm will download the latest version of that package from the registry, regardless of which Node version you have installed.
We can actually test this using the #angular/cli example. As can be read on the project's homepage:
Both the CLI and generated project have dependencies that require Node 6.9.0 or higher, together with NPM 3 or higher.
So if I downgrade my Node version to the final 5.x release:
$ nvm install 5.12.0
Downloading and installing node v5.12.0...
Downloading https://nodejs.org/dist/v5.12.0/node-v5.12.0-linux-x64.tar.xz...
######################################################################## 100,0%
Computing checksum with sha256sum
Checksums matched!
Now using node v5.12.0 (npm v3.8.6)
And then run npm install #angular/cli -g, you can see that npm couldn't care less which version of Node I am running:
$ npm ls -g --depth=0
/home/jack/.nvm/versions/node/v5.12.0/lib
├── #angular/cli#1.7.1
└── npm#3.8.6
As a side note, you might want to look into using a Node version manager, as this will mean you don't need to run npm install using sudo. Here's a good guide if you're interested. This is especially relevant in regard to the recent npm shenanigans (ref.: Unlucky Linux boxes trampled by NPM code update, patch zapped)

How to install beta version via npm

I need to install a beta version 3.0.0 of react-docgen.
Is it possible to install a beta version via npm?
Yes, it's possible to install a beta version using the # symbol. For example to install react-docgen (v3.0.0-beta7) run the following command:
npm install -g react-docgen#3.0.0-beta7
Further information about installing specific versions can be found in the npm-install documentation.
The syntax for npm install includes both of the following overloads:
npm install [<#scope>/]<name>#<tag>
npm install [<#scope>/]<name>#<version>
For tag, you can specify either #latest and #beta like this:
npm install #11ty/eleventy#latest # latest stable release
npm install #11ty/eleventy#beta # latest beta release
Interestingly, if you don't specify a tag, the default, set by your npm config, is #latest
You can also find out which versions are available by running npm view
npm view #11ty/eleventy
To install a specific beta version:
npm install -g react-docgen#3.0.0-beta7
//or
yarn global add react-docgen#3.0.0-beta7 // if you use yarn
P.S.:
In order to see all versions of a package (including alpha, beta , pre-release, release):
npm show <package> versions --json
Example:
npm show react-docgen versions --json
You can also install a certain version using a tag!
npm install <name>#<tag>
In order to see which tags are available for this package, you have to run:
npm view react-docgen
A tag can be used when installing packages as a reference to a version
instead of using a specific version number (alias)

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

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>

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.