Importing npm Modules in Node.js

When we install Node.js, we also get npm. npm is a package manager that allows you to install and use third-party npm libraries in your code. This opens up a world of possibilities, as there are npm packages for everything from email sending to file uploading

Initializing npm

Our Node.js application needs to initialize npm before npm can be used. we can run npm init from the root of your project to get that done. That command will ask us a series of questions about the project and it’ll use the information to generate a package.json file in the root of our project


“name”: “notes-app”,
“version”: “1.0.0”,
“description”: “”,
“main”: “app.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1″
“author”: “”,
“license”: “ISC”,

Installing an npm Module

This is done using the npm command which was set up when Node.js was installed. we can use npm install to install a new module in our project

npm install validator

Importing an npm Module

npm modules can be imported into our script using require. To load in an npm module, pass the npm module name to require

const validator = require(‘validator’)

Leave a Comment