Introduction to Node.js and npm
Learn about Node.js, npm, and npx.
Beginner to IntermediateUpdated
Nodejs
- Node.js is a runtime environment that allows JavaScript to run outside the browser.
- It is commonly used to build backend servers, tools, and full-stack applications.
- When you install Node.js, npm (Node Package Manager) gets installed automatically.
- Node.js, npm, and npx together form the foundation for modern JavaScript development.
- Download Node.js from here
Npm
- npm is used to install and manage packages (libraries, tools, frameworks) for your project.
- A package is a reusable set of code shared by developers — like React, Express, Lodash, etc.
- npm downloads packages from the npm registry, which is a public online database.
- You search for packages on npmjs.com
- Every project that uses npm has a file called package.json.
- package.json lists all the dependencies (packages) your project needs.
- We have two files in our project: package.json and package-lock.json.
- package-lock.json is used to keep track of the exact versions of installed packages.
- When you run npm install, npm reads package.json and installs all listed dependencies.
- 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.
- This allows each project to have its own set of packages without affecting other projects.
- You can install a package using npm install package-name.
- You can install a specific version using: npm install package-name@version.
Example: Install React version 18.2.0 npm install react@18.2.0- npm uses semantic versioning — like 1.2.3 (major.minor.patch) — to manage package versions.
- Global installation is done with: npm install -g package-name.
- npm keeps a local cache of downloaded packages to speed up future installs.
You can clear cache using: npm cache clean --forceThis will remove all the cached packages and clear the cache.
Npx
- npx is a tool that comes with npm — it runs a package directly without installing it globally.
Example: Create a new React project npx create-react-app my-app- npx avoids polluting your global system with packages you only need temporarily.
Example: Check globally installed packages npm list -g --depth=0- 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)