How to install npm on linux

Install and Use the Node Package Manager (NPM) on Linux

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

The Node Package Manager (NPM) is the most widely-used package manager for JavaScript projects and includes Node.js by default. This guide walks you through installing NPM and getting started using it on your Linux system.

What is NPM?

NPM is the default package manager for Node.js projects. NPM includes a command-line tool (CLI) that give you access to the NPM package registry. The registry stores the numerous JavaScript packages made available through the NPM CLI, along with their metadata. The NPM website gives you an easy way to search for JavaScript packages and read information about them. The package.json file that is generated by the NPM CLI helps you manage project dependencies. It also ensures consistent project installations across environments.

NPM vs Yarn

The Yarn package manager has become a popular alternative to NPM. Both NPM and Yarn use the NPM registry to get packages and package information, and the commands in each tool are pretty similar.

One advantage to NPM is that it is the default package manager for Node.js. If you have Node.js installed on your system, you automatically have access to NPM. This means that once you have Node.js installed, you immediately have access to all the functionality available through the NPM package registry.

NPM is also the most popular Node.js package manager. With that popularity comes a wide range of coverage. You are more likely to find Node.js projects and examples that use NPM than you are to find ones using Yarn.

Yarn was originally designed to address performance and security concerns in NPM. And while Yarn still outshines NPM in terms of speed, NPM has made vast security improvements that put it about even with Yarn.

You can learn more about Yarn in our How to Install and Use the Yarn Package Manager guide.

To learn how to install Node.js, jump to the How to Install NPM section of this guide.

How to Install or Update NPM

The steps in this section show you how to install NPM along with Node.js. It also gives you methods for updating your NPM installation.

Читайте также:  Добавить пользователя ssh линукс

How to Install NPM

Since NPM is packaged with Node.js, you just need to install Node.js. The installation path recommended by NPM is using a the Node Version Manager (nvm). This version manager helps you avoid permissions issues and version conflicts with NPM packages. To install nvm follow the steps in our How to Install and Use the Node Version Manager NVM guide.

Using nvm, you can install the current stable version of Node.js, and its accompanying version of NPM using the following command:

To verify your NPM installation, check for the installed npm version.

How to Update NPM

When working with nvm to manage your Node.js versions, updating your NPM version requires that you update your Node.js version. To make sure you are on the latest version of NPM, use NVM’s install command to install the current stable Node.js version.

Then, tell NVM to use the latest version.

If you want to update NPM, without updating Node.js, use nvm’s dedicated command.

How to Install Packages with NPM

The sections that follow show you how to install and use NPM packages.

Most often, you install NPM packages for a specific project. Use commands below to create an example NPM project to follow along with this guide.

Create a new directory for the example project and move into the new directory.

mkdir ~/example-app cd ~/example-app 

Initialize the new NPM project.

NPM prompts you for information about your project. Use the defaults for this example. The result is an initial package.json file representing your project.

Install Packages to a Project

NPM provides two main ways for installing specific packages to your project.

    You can install a specific package with NPM’s install command. In the example below, NPM installs the latest stable version of the package. The example installs the Express web application framework.

You can even specify a version range for the package you want to be installed. Place the version expression between quotes, and precede the version number with the comparison operator you want to use. You can use multiple version constraints, separating them with spaces.

Package.json

The npm init command creates a package.json file in the project’s base directory. Any packages installed in the project are reflected in this file. The package.json is a reflection of your project’s dependency structure.

The file below is an example package.json that results from the basic npm install express command (from the above section).

Notice that the express package is listed under dependencies . The ^ indicates that a package version compatible with version 4.17.1 must be installed for this project.

Take a look at NPM’s package.json documentation, specifically the dependencies section for more information about the syntax used for parsing package versions. The same syntax can be used when specifying a version, or a version range, for the install command.

Install Dependencies for an Existing Project

You may come across an existing project, with its own package.json , that you want to get up and running. Typically, you first need to install the project’s dependencies. You can do this by running the install command from that base directory without specifying a package. For example:

Читайте также:  Графический интерфейс сервера linux

NPM uses the project’s package.json to determine which packages are needed and which versions of those packages are compatible with the project. If the project has a package-lock.json , NPM also uses that to further ensure compatibility with the dependencies that get installed.

Install Packages Globally

You may want to install some packages globally. Packages installed in this way are available anywhere on the system, not just within a particular project.

You can accomplish this with NPM’s global ( -g ) flag, as shown below:

You can also use the -g option with the commands in the next two sections. This option allows you to uninstall and update global packages.

How to Remove Packages with NPM

You can uninstall an NPM package with the uninstall command.

The uninstall command updates the package.json to reflect that the project no longer depends on the uninstalled package.

How to Update Packages with NPM

You can use the command below to update all of a project’s packages to their latest compatible versions.

NPM references the version constraints given in the project’s package.json file to ensure that updates do not contradict your project’s specified version.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on Friday, September 3, 2021.

Источник

LearnHow to Install Node.js and NPM on Linux

JavaScript is used everywhere today. From its birthplace in the Netscape browser in the mid-nineties, it’s made its way into all modern web browsers, onto mobile phones, and into electronics to make full-fledged web applications on the server-side with Node.js.

Node.js is non-blocking, which means it’s ideal for creating real-time web applications such as chat servers, analytics, collaboration tools, and interactive games.

In order to create such applications, you’ll need NPM a package manager for Node. It allows you to download and install open source JavaScript libraries that help you make awesome applications.

Prerequisites Before Installing

  • You should have some familiarity with the Linux terminal since you’ll need to use it to install and test Node and NPM. You’ll also need the terminal to use Node.js and NPM.
  • Dependencies. You need to install a number of dependancies before you can install Node.js and NPM.
  • Ruby and GCC. You’ll need Ruby 1.8.6 or newer and GCC 4.2 or newer.
    • For Ubuntu or Debian-based Linux distributions, run the following command in your terminal: sudo apt-get install build-essential curl git m4 ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev Then select Y to continue and wait for the packages to be installed.
    • For Fedora based Linux distributions run the following command in your terminal application: sudo yum groupinstall ‘Development Tools’ && sudo yum install curl git m4 ruby texinfo bzip2-devel curl-devel expat-devel ncurses-devel zlib-devel Then select Y to continue and wait for the packages to be installed.

    Once Linuxbrew is installed, you’ll need add the following 3 lines to your .bashrc or .zshrc file:

     export PATH="$HOME/.linuxbrew/bin:$PATH" export MANPATH="$HOME/.linuxbrew/share/man:$MANPATH" export INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH" 

    Why Homebrew/Linuxbrew?

    You may be asking why you should use a third-party package manager. Here are some advantages:

    • Can install software to a home directory and so does not require sudo
    • Install software not packaged by the native distribution
    • Install up-to-date versions of software when the native distribution is old
    • Use the same package manager to manage both your Mac and Linux machines

    Installation

    Installing Node.js and NPM is pretty straightforward using Linuxbrew, the Linux port of Homebrew. It handles downloading, unpacking, compiling, and installing Node and NPM on your system. After you have Linuxbrew installed, the whole process should only take you a few minutes.

    1. Open up your terminal and type brew install node .
    2. Sit back and wait. Homebrew has to download some files, compile and install them. But that’s it.

    How to Check Node.js on Linux

    Make sure you have Node and NPM installed by running simple commands to see what version of each is installed:

    • Test Node.js. To see if Node.js is installed, type node -v in the terminal. This should print the version number, so you’ll see something like this: v0.10.35 .
    • Test NPM. To see if NPM is installed, type npm -v in the terminal. This should print the version number, so you’ll see something like this: 2.1.17

    node-install-check

    How to Update Node and NPM on Linux

    New versions of Node and NPM are released frequently. You can use Homebrew to update the software it installs.

    1. Make sure Homebrew has the latest version of the Node package. In Terminal type:
      brew update
    2. Upgrade Node: brew upgrade node

    How to Uninstall Node and NPM

    You can also use Homebrew to uninstall packages that it installed:

    Other Platforms

    Thanks to Dave McFarland, as he wrote some installation guides to install Node.js and NPM on Mac (which this guide heavily borrows from) and Windows.

    Conclusion

    Now that you have Node.js and NPM installed, you can build an awesome application with Node.js, or use one of the Node packages like Grunt or Gulp to improve your front-end workflow.

    If you’re looking to take your JavaScript coding to another level, check out the Treehouse Techdegree in Full Stack JavaScript. Our faculty of tech professionals guide learners like you from mastering the fundamentals of JS to polishing the skills of a job-ready developer. Try our program out with a free seven-day trial today.

    Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you’ve been dreaming about.

    Источник

Оцените статью
Adblock
detector