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