Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
How to install modules without npm in node.js ?
Next article icon

How To Use Node Modules with npm and package.json

Last Updated : 12 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

NodeJS is a powerful runtime for server-side JavaScript & these modules are reusable pieces of code that can be easily imported and used in NodeJS applications. npm (Node Package Manager) is the default package manager for Node JS and is used to install, manage, and publish NodeJS packages. This article will guide you through the process of using NodeJS modules with npm and package.json, covering various approaches, and steps to create an application, updating dependencies in package.json, and providing examples with outputs.

Table of Content

  • Key Features
  • 1. Initialize a Node.js project
  • 2. Install Node.js modules
  • 3. Save dependencies to package.json
  • 4. Use installed modules in your code
  • 5. Include package.json in your project
  • 6. Install dependencies from package.json

Key Features:

  • Comprehensive coverage of using NodeJS modules with npm.
  • Clear explanations and examples for easy understanding.
  • Practical steps to create an application and manage dependencies.
  • Visual aids such as GIFs or screenshots for better illustration.
  • References for additional reading and learning.

1. Initialize a Node.js project:

If you haven't already, you need to initialize your NodeJS project. Open a terminal or command prompt, navigate to your project directory, and run:

npm init -y

This command initializes a new NodeJS project with default settings and creates a package.json file.

2. Install Node.js modules:

To install Node.js modules (packages) using npm, run:

npm install <package-name>

Replace <package-name> with the name of the package you want to install. For example:

npm install express

This will install the Express framework package.

You can also install multiple packages at once by separating them with spaces:

npm install express mongoose body-parser

3. Save dependencies to package.json:

When you install packages using npm, you can choose to save them as dependencies in your package.json file. There are two types of dependencies: dependencies (packages required for your application to run) and devDependencies (packages required for development purposes only, such as testing frameworks or build tools).

To save a package as a dependency:

npm install <package-name> --save

To save a package as a devDependency:

npm install <package-name> --save-dev

For example:

npm install jest --save-dev

4. Use installed modules in your code:

Once you've installed the desired modules, you can use them in your Node.js code. For example, if you've installed Express, you can create an Express server like this:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
res.send('Hello, world!');
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

5. Include package.json in your project:

Ensure that your package.json file is included in your project directory. This file contains metadata about your project and lists its dependencies.

6. Install dependencies from package.json:

If you're sharing your project with others or deploying it to a server, you can install all the dependencies listed in your package.json file by running:

npm install

This command will install all dependencies (both regular and devDependencies) listed in your package.json file.

That's it! You've now successfully used NodeJS modules with npm and package.json. You can continue adding more dependencies as needed and managing them through your package.json file.


Next Article
How to install modules without npm in node.js ?

B

banu3513
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • Node-npm

Similar Reads

  • How to add a non-npm dependency to package.json?
    One of the great features of the npm ecosystem is the ability to install and manage packages from the npm registry. These dependencies are listed in the "dependencies" section of the project's package.json file.  However, sometimes you may need to use a dependency that isn't available through npm, s
    5 min read
  • How to list npm user-installed packages in Node.js?
    What is Node.js? Node.js is an open source and cross-platform runtime environment for executing JavaScript code outside of a browser. Click here for more. What is npm? Here, "npm" stands for "Node Package Manager" which is the package manager for Node.js and serves as a command-line utility for inte
    2 min read
  • How to install modules without npm in node.js ?
    We can install modules required for a particular project in node.js without npm, the recommended node package manager using yarn. Yarn is a wonderful package manager. Like npm, if you have a project folder with package.json containing all the required dependencies mentioned for the project, you can
    3 min read
  • Introduction to packages and modules in npm
    The Node Package Manager (npm) serves as a tool for the JavaScript programming language functioning both as a command line utility and package manager. It is the choice for managing dependencies and sharing packages within the NodeJS environment. The public npm registry acts as a centralized platfor
    4 min read
  • How to Find the Version of an Installed NPM Package in Node.js ?
    NPM is the default package manager for Node.js. NPM manages both internal and external packages or modules used in various Node.js applications. The default packages in NPM may not fulfil all the needs of a developer, so we often require external packages. These can be installed either locally in a
    2 min read
  • How to use External Modules and NPM in a project ?
    Need for External Modules: For a large JavaScript application, it becomes difficult and messy to write the whole code in just one JavaScript file. This is where CommonJS comes into the picture and this CommonJS format defines a module format that can be used up for breaking your JS application into
    3 min read
  • How to read and write JSON file using Node ?
    Node JS is a free and versatile runtime environment that allows the execution of JavaScript code outside of web browsers. It finds extensive usage in creating APIs and microservices, catering to the needs of both small startups and large enterprises. JSON(JavaScript Object Notation) is a simple and
    3 min read
  • How to use EcmaScript Modules in Node.js ?
    Using ECMAScript Modules (ES Modules or ESM) in Node.js allows you to take advantage of modern JavaScript syntax for organizing and managing your code. ECMAScript Modules provide a more structured and standardized way to work with modules compared to CommonJS, which has been traditionally used in No
    2 min read
  • How to Update Local Package in NPM?
    Updating the local packages in NPM is a common task for the developers. Whether it is for bug fixes, new features, or security patches. Keeping your dependencies up to date is essential. These are the following approaches to updating the local package in NPM: Table of Content Update to the Latest St
    4 min read
  • How to Change the Node.js Module Wrapper ?
    Changing the Node.js module wrapper involves customizing the way modules are wrapped by modifying the Module.wrap method. This allows for altering the function wrapper used in module loading. Module Wrapper FunctionUnder the hood, NodeJS does not run our code directly, it wraps the entire code insid
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences