Warning: session_start(): open(/tmp/sess_c44367f02f6f5ee5bea37f466ca20ec2, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
npm Tutorial For Beginners: Getting Started With Node.js Package Management - LearnShareIT

npm Tutorial For Beginners: Getting Started With Node.js Package Management

npm Tutorial For Beginners: Getting Started With Node.js Package Management

It is almost impossible to manually resolve and manage packages for your Node.js projects. This npm tutorial for beginners will show you how to use this package manager to get it done.

What Is npm?

npm is the default package manager for Node.js. It is also the name of a repository where JavaScript packages are published. This registry and the companion package manager are both managed by npm, Inc – a subsidiary of GitHub, which in turn is owned by Microsoft.

Node.js is a JavaScript runtime environment originally created by Ryan Dahl in 2009. It allows developers to write back-end software in JavaScript. A year later, the package manager npm was introduced.

The purpose of npm (the online repository) is to help developers publish their Node.js packages so others can reuse them in their own applications. Thanks to the utility of the same name, they can easily install, update, and uninstall these packages in their local system through the command-line interface.

Plenty of Node.js applications and libraries are newly uploaded or updated in the npm registry every day. Using npm to take advantage of those packages is a must-have skill for any Node.js developer.

Install npm

The npm program should already come with your Node.js installation. You can verify its existence with this command:

npm -v

When npm has been installed and is accessible to your terminal app, it should print the installed version:

8.19.2

If the output returns an error indicating the shell can’t find the npm command, you should install nvm (Node Version Manager) first and then use it to install Node.js and npm.

On Linux and macOS, run one of these two commands to install nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

On Windows, get the nvm-set.exe file from this repository, and run it to install nvm.

Open a new terminal window, then check the version of your nvm installation:

nvm -v
0.39.2

Use this command to install the stable version of Node.js:

nvm install stable

Verify our Node.js installation:

node -v
npm -v

How npm Works

package.json

The package.json file is the heart of any Node.js application, including Node.js modules or packages. This JSON file is located in the root directory of a project, containing its metadata, such as its name, description, and dependencies.

Package managers like npm need these pieces of information to identify the package and manage its dependencies. Without the package.json file, npm wouldn’t be able to get every package required to run your Node.js project, start it, run other scripts, or publish it to the registry.

As you will learn below, you can create and manage the package.json file of your project with npm as well.

package-lock.json

npm also maintains a lockfile named package-lock.json in the same place as package.json. While package.json provides the high-level overview of your project, package-lock.json is where npm keeps the snapshots of its dependency tree.

You should never edit this file manually. It works with the package.json file to keep track of your project’s dependencies.

node_modules

When you ask npm to install certain packages or resolve your project’s dependencies, they will be placed in the directory named “node_modules” at the root of your project.

In particular, npm will read and update package.json and package-lock.json if needed, download and install the packages into node_modules, and finally, run an audit of these packages to find security vulnerabilities.

npm Tutorial For Beginners

Initialize A Node.js Project

You can use the npm init command to create a package.json in a brand-new project. It should be located in an empty directory, where every part of your project is saved locally.

Switch the current working directory to your project, and run this command:

npm init

It will ask you several questions about your project, such as its name, author, and version. All the fields are optional, and you can just hit Enter to use the default values npm gives you.

By default, npm will use “npm” as the project name, “1.0.0” as its version, “index.js” as the main script, and “ISC” as the software license. Enter the 

Here is how simple a package.json file may look:

{
    "name" : "learnshareit",
    "description" : "npm tutorial for beginners",
    "version" : "0.0.1",
    "dependencies": {
        "express": "expressjs/express"
    }
}

This file sets “learnshareit” as the name of the project and “express” as its sole dependency.

At this point, npm still hasn’t installed any package to your project.

Note: if you want to generate a package.json file without npm asking any questions, run this command:

npm init -v

Resolve Dependencies

After creating a package.json file with your project’s required packages in it, you can run the npm install command with no arguments to install all those dependencies to the node_modules directory:

npm install

By default, npm will parse the package.json file and find the packages listed in the dependency field. With the example above, npm will install the Express framework, all of its dependencies, dependencies of these dependencies, and so on, from the npm registry.

Output like this means the install process has run without errors:

added 57 packages, and audited 58 packages in 6s
found 0 vulnerabilities

npm also creates the package-lock.json to save the current state of the dependency tree.

Install Packages

While developing your Node.js project, you can always install packages from the npm registry when your project needs them.

For example, use this command to install the Axios library:

npm install axios

By default, npm install enables the –save option. This means the above package will also be added as a dependency to the package.json file.

    "dependencies": {
        "axios": "^1.1.3",

 Set this option to false if you don’t want npm to automatically update package.json:

npm install --save=false axios

Update Packages

Run this command to update all the packages listed in package.json (and their dependencies) to the latest version:

npm update

This operation also installs any missing packages.

Uninstall Packages

The npm uninstall command will remove a package and its dependencies from your project:

npm uninstall axios

npm will also remove the package from the package.json file.

Tutorials

Summary

The package manager npm can help you install dependencies for your Node.js projects. This npm tutorial for beginners has shown you the basic operations for a new project. Follow other guides of ours to learn more about npm and its role in Node.js projects.

Leave a Reply

Your email address will not be published. Required fields are marked *