DEV Community

Rajat Verma
Rajat Verma

Posted on

How to use NPM (and import/export modules) in JavaScript

Alt Text
If you are familiar with JavaScript or Web Development then you must have heard about npm. NPM helps us to manage packages and dependencies in our projects. So, while learning a JavaScript framework the knowledge of npm would be really useful to learn it in an easier manner.

In this series of articles, we aimed to cover the following topics:

  1. Important ES6 Features
  2. Objects and Array methods
  3. Asynchronous JavaScript and Fetch API
  4. NPM and import/export modules in JavaScript (this article)

Let’s first start with NPM:

NPM

What is NPM?

NPM is the default package manager for node. It is used to install, share, and manage javascript packages in a project.
NPM has three components:

  1. The website (Using the website we can find, share, and view packages)
  2. The Command Line Interface (CLI) (The CLI is the component that helps us in managing our packages)
  3. The registry (The npm registry is the database where all the packages exist, we can download packages published by other developers and can also publish our own packages to the registry)

Note:

  • NPM can also be used to publish and manage private packages.
  • A package is simply a program that performs one or more operations.

How to Install npm?

NPM comes pre-installed with node.js. So, you don’t need to worry about installing it manually, you just have to install node.js on your system.

To install node.js, visit https://nodejs.org/en/download, and install its LTS (Long Term Support) version.
After installation, use the commands shown below to check if they are installed:

// to check nodejs's version
node -v or node --version  
// to check npm's version
npm -v or npm --version
Enter fullscreen mode Exit fullscreen mode

This will result in something like this:
image.png

package.json

The package.json file is like the manifest of your project. It makes it easier to install and manage packages. It consists of all the metadata of the project that will be useful while sharing the project with other developers.
According to the official docs:

A package.json file:

  1. lists the packages your project depends on
  2. specifies versions of a package that your project can use using semantic versioning rules
  3. makes your build reproducible, and therefore easier to share with other developers

Read more>>>

Discussion (0)