I ran into a problem today when I decided to use npm update -g
in a nodejs app directory. This was a bad decision, because it caused me to run into a common and disastrous problem with npm.
The solution is to reinstall nodejs and npm. Unfortunately when I go to install npm using sudo apt-get install npm
it doesn't work.
The full error in the terminal is here:
Some packages could not be installed. This may mean that you have requested
an impossible situation or if you are using the unstable distribution that
some required packages have not yet been created or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
npm : Depends: nodejs but it is not going to be installed
Depends: node-abbrev (>= 1.0.4) but it is not going to be installed
Depends: node-ansi but it is not going to be installed
Depends: node-archy but it is not going to be installed
Depends: node-block-stream but it is not going to be installed
Depends: node-fstream (>= 0.1.22) but it is not going to be installed
Depends: node-fstream-ignore but it is not going to be installed
Depends: node-github-url-from-git but it is not going to be installed
Depends: node-glob (>= 3.1.21) but it is not going to be installed
Depends: node-graceful-fs (>= 2.0.0) but it is not going to be installed
Depends: node-inherits but it is not going to be installed
Depends: node-ini (>= 1.1.0) but it is not going to be installed
Depends: node-lockfile but it is not going to be installed
Depends: node-lru-cache (>= 2.3.0) but it is not going to be installed
Depends: node-minimatch (>= 0.2.11) but it is not going to be installed
Depends: node-mkdirp (>= 0.3.3) but it is not going to be installed
Depends: node-gyp (>= 0.10.9) but it is not going to be installed
Depends: node-nopt (>= 2.1.1) but it is not going to be installed
Depends: node-npmlog but it is not going to be installed
Depends: node-once but it is not going to be installed
Depends: node-osenv but it is not going to be installed
Depends: node-read but it is not going to be installed
Depends: node-read-package-json (>= 1.1.0) but it is not going to be installed
Depends: node-request (>= 2.25.0) but it is not going to be installed
Depends: node-retry but it is not going to be installed
Depends: node-rimraf (>= 2.2.2) but it is not going to be installed
Depends: node-semver (>= 2.1.0) but it is not going to be installed
Depends: node-sha but it is not going to be installed
Depends: node-slide but it is not going to be installed
Depends: node-tar (>= 0.1.18) but it is not going to be installed
Depends: node-which but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
What can I do to get npm properly installed and running again?
Thanks.
Best Answer
Update
Regarding William Entriken's comment below this answer, there is a better way of installing Node.js natively in Ubuntu as a snap package.
Node.js is available as a snap package in all currently supported versions of Ubuntu. Specific to Node.js, developers can choose from one or more of the currently supported releases and get regular automatic updates directly from NodeSource. Node.js versions 6, 8, 9, 10, 11, 13 and 14 are currently available, with the Snap Store being updated within hours or minutes of a Node.js release.
Node can be installed with a single command, for example:
The node snap can be accessed by the command
node
, for example:An up-to-date version of npm will installed as part of the node snap. npm should be run outside of the node repl, in your normal shell. After installing the node snap run the following command to enable npm update checking:
Replace
your-username
in the above command with your own username. Then runnpm -v
to check if the version of npm is up-to-date. As an example I checked that npm was up-to-date, checked the version of an already installed package named yarn with the commandnpm list yarn
and then updated the existing yarn package to the latest version with the commandnpm update yarn
Users can switch between versions of Node.js at any time without needing to involve additional tools like nvm (Node Version Manager), for example:
Users can test bleeding-edge versions of Node.js that can be installed from the latest edge channel which is currently tracking Node.js version 12 by switching with:
This approach is only recommended for those users who are willing to participate in testing and bug reporting upstream.
Original answer
To install the latest LTS versions of Node.js (nodejs) and Package manager for Node.js (npm) in all currently supported versions of Ubuntu, open the terminal and run the following commands:
If you are copying these three commands, don't neglect the hyphen at the end of the second command.
The
nodejs
package contains thenodejs
binary as well asnpm
, so you don't need to installnpm
separately. However, in order for somenpm
packages to work (such as those that require building from source), you will need to install thebuild-essential
package:LTS Plan
New semver-major releases of Node.js are cut from
master
every six months. New even-numbered versions (e.g. v6, v8, v10, etc.) are cut in April. New odd-numbered versions (e.g. v5, v7, v9) are cut in October.When a new odd-numbered major release is cut, the previous even-numbered major version transitions to the Long Term Support plan.
Every major version covered by the LTS plan will be actively maintained for a period of 18 months from the date it enters LTS coverage. Following those 18 months of active support, the major version will transition into "maintenance" mode for 12 additional months.
non-LTS installation
Current to March, 2017 the commands to install the latest non-LTS version of Node.js (v8 at the time this was posted) are as follows:
Node.js LTS Schedule