Skip to content

Introduction to Node.js and npm

Learn about Node.js, npm, and npx.

Beginner to IntermediateUpdated

Nodejs

  1. Node.js is a runtime environment that allows JavaScript to run outside the browser.
  2. It is commonly used to build backend servers, tools, and full-stack applications.
  3. When you install Node.js, npm (Node Package Manager) gets installed automatically.
  4. Node.js, npm, and npx together form the foundation for modern JavaScript development.
  5. Download Node.js from here

Npm

  1. npm is used to install and manage packages (libraries, tools, frameworks) for your project.
  2. A package is a reusable set of code shared by developers — like React, Express, Lodash, etc.
  3. npm downloads packages from the npm registry, which is a public online database.
  4. You search for packages on npmjs.com
  5. Every project that uses npm has a file called package.json.
  6. package.json lists all the dependencies (packages) your project needs.
  7. We have two files in our project: package.json and package-lock.json.
  8. package-lock.json is used to keep track of the exact versions of installed packages.
  9. When you run npm install, npm reads package.json and installs all listed dependencies.
  10. The packages are stored locally in a folder named node_modules, where all the installed code lives and it is created automatically when you run npm install.
  11. This allows each project to have its own set of packages without affecting other projects.
  12. You can install a package using npm install package-name.
  13. You can install a specific version using: npm install package-name@version.
  14. Example: Install React version 18.2.0
    npm install react@18.2.0
  15. npm uses semantic versioning — like 1.2.3 (major.minor.patch) — to manage package versions.
  16. Global installation is done with: npm install -g package-name.
  17. npm keeps a local cache of downloaded packages to speed up future installs.
  18. You can clear cache using:
    npm cache clean --force
    This will remove all the cached packages and clear the cache.

Npx

  1. npx is a tool that comes with npm — it runs a package directly without installing it globally.
  2. Example: Create a new React project
    npx create-react-app my-app
  3. npx avoids polluting your global system with packages you only need temporarily.
  4. Example: Check globally installed packages
    npm list -g --depth=0
  5. npm installs the package into your project, while npx runs it directly without saving it.

Did this help?

Practise this topic in a mock interview with Lala on SevyDevy.

Practise on SevyDevy ↗ (opens in a new tab)
Next →