What is npm? The JavaScript package manager explained

0


[ad_1]

Packages are an essential part of many programming languages, and JavaScript is no exception. They can be used to add various functionality to your application or script, from creating a web server to sending emails.

Without packages, you will have to reinvent the wheel by programming the same functionality into each of your projects that require it. Interested? This guide explains how to install and use packages in JavaScript with npm.

What is Node Package Manager (npm)?

JavaScript uses Node Package Manager, often abbreviated as npm, as its package manager and package repository. Node is the abbreviation for Node.js, the JavaScript runtime environment used to execute JavaScript code outside of the browser.

With over a million packages hosted on the npm website, developers can search and browse the huge catalog of JavaScript libraries. Some of these packages are downloaded over 10 million times per week. The website provides information about all the packages hosted on it, such as source code, documentation, version number, and unzipped size.

Along with the website, npm also provides a command line tool that allows developers to install or uninstall these packages.

The npm command line tool is built into Node.js. Therefore, it is essential to download Node.js to your machine before using JavaScript packages.

Visit the official Node.js website to download the appropriate version based on your operating system. Once downloaded, follow the onscreen instructions to complete the installation process.

For more information, see our guide on installing Node.js on Windows. If you plan to install multiple versions of Node.js on your Linux machine, tools such as NVM can help you manage multiple installations of Node.js.

To verify your installation, open the command prompt on Windows or the terminal on Linux and macOS and run the following commands:

node --version
npm --version

If the installation was successful, the terminal will display the installed version of Node.js and npm.

Check the version of Node.js and NPM

Installing packages

The npm command line tool makes installing packages on your JavaScript or Node.js projects extremely easy with its single line command. Open the command prompt or terminal in your project directory and run the following command:

npm install 

Install a package with NPM

You can also install multiple packages using a single command by separating the package names with a space like this:

npm install    ... 

Installing multiple packages with NPM

Using installed packages

Once you have installed the packages using the npm install command, it is time for you to start using them. You may notice that a new folder named node_modules and 2 new files, package.json and package-lock.json, were generated automatically. You don’t have to worry about these files. npm generates them to keep track of your project’s dependencies.

To use the installed packages, you will need to require them or import them into your JavaScript code. The syntax for doing this can be one of two commands depending on the version of JavaScript you are using:

const package = require('package-name');
import package from 'package-name';

Using packages installed in JavaScript

You can consult the documentation for the package you are using on the npm website for the exact syntax.

Uninstalling packages

Uninstalling packages is as easy as installing them. The command to uninstall the packages from your project is:

npm uninstall 

Uninstall a single package

Just like the install command, you can also uninstall multiple packages with a single command via:

npm uninstall   ... 

Uninstalling multiple packages

Efficient use of packages

While packages can make your life as a developer easier, they also create a dependency between your project and the packages you use. Therefore, it is recommended that you give it some thought before installing multiple packages.

Rather than dramatically modifying the installed packages to suit your needs, you can also create your own packages and publish them for free to npm. With an appropriate design template, you can create packages for you and your team to use in future projects and make your code reusable.

Image Credit: Ferenc Almasi on Unsplash


javascript-design-patterns-2

How to Create Reusable Code in JavaScript Using Design Templates

Understanding how to use design templates will allow you to use reusable code in JavaScript. Here’s what you need to know.

Read more


About the Author

[ad_2]

Share.

Comments are closed.