npm and package.json — EACCES Permission Error Without sudo
- npm is a package manager — it lets you install, share, and manage reusable code. It installs automatically with Node.js, so you don't need to set it up separately.
- package.json is your project's identity card and shopping list — it records the project name, version, scripts, and every package the project depends on.
- dependencies are packages your app needs to run in production (like express or axios). devDependencies are tools you only need while building or testing (like jest or eslint). Getting this wrong wastes resources on production servers.
- npm is the Node Package Manager — a registry, CLI, and file format combined
- package.json is your project's manifest: name, version, dependencies, scripts
- dependencies are for runtime; devDependencies are for development tools
- npm install downloads packages into node_modules and records them in package.json
- Semantic versioning (^1.4.2) controls which updates are safe to accept
- The package-lock.json locks every sub-dependency version — never delete it
npm Quick Debug Commands
Package not found / version mismatch
npm list --depth=0npm view <package> versionsGlobal command (like `create-react-app`) not found
npm list -g --depth=0which <command> (or `where` on Windows)Slow install / network timeouts
npm cache verifynpm config set registry https://registry.npmmirror.com (or your mirror)Lockfile conflicts in Git (package-lock.json merge conflicts)
git checkout --theirs package-lock.json (or yours)npm install (npm resolves conflicts automatically)Production Incident
/usr/lib/node_modules) requires root access. Running with sudo changes ownership of installed packages to root, so later operations by non-root users fail.npm config set prefix ~/.npm-global, then add export PATH=~/.npm-global/bin:$PATH to your profile. Or use nvm to manage Node versions entirely in user space.Production Debug GuideSymptom → Action guide for the npm errors that waste the most time on a team.
npm config get prefix. If it's /usr/lib/node_modules, reconfigure to ~/.npm-global and update your PATH. Or use nvm instead.require() fails with MODULE_NOT_FOUND→Check that the package appears in node_modules or in package.json under dependencies. Verify your require path is correct. If using multiple packages, check for hoisting issues in npm v6 vs v7+.npm install — "conflicting peer dependency"→Use npm ls <package> to see the tree. Common fix: add a --legacy-peer-deps flag temporarily, or better, manually align the conflicting versions. npm v7+ enforces peer deps strictly.npm audit fix to apply safe updates automatically. For breaking changes, manually update the offending packages. Use npm audit --json to get details.Every professional JavaScript project you'll ever work on uses npm. It powers over 2 million packages and is downloaded billions of times every week. When you land a junior dev role and someone says 'just run npm install', you need to know exactly what's happening under the hood — not just how to type the command.
Before npm existed, sharing code between projects was painful. You'd copy-paste files manually, lose track of versions, and when a bug fix came out you'd have no way of knowing. npm solves all of that by giving every project a single source of truth: the package.json file. It tracks what external code your project depends on, what version of it you need, and even how to run your app.
By the end of this article you'll be able to create a project from scratch, install packages, understand every key field in package.json, and explain the difference between a dependency and a devDependency — the question that trips up a surprising number of candidates in junior dev interviews.
What npm Actually Is and Why It Was Built
npm stands for Node Package Manager. It ships automatically when you install Node.js, so if Node is on your machine, npm is already there.
The core job of npm is simple: let developers share and reuse code. That reusable chunk of code is called a 'package'. A package could be anything — a tool to format dates, a full web framework, a utility to send emails. Instead of writing all of that yourself, you pull in someone else's tested, maintained package.
npm has three parts that work together. First, there's the registry — a huge online database at registry.npmjs.org where packages live. Second, there's the CLI (command-line interface) — the npm commands you type in your terminal. Third, there's the website (npmjs.com) where you can search for and read about packages.
Think of the registry as the supermarket's warehouse, the CLI as your shopping trolley, and npmjs.com as the store's website where you browse before you go. You don't need to think about all three at once — most of the time you're just typing commands in the terminal and npm handles the rest.
# First, verify that Node.js is installed on your machine # This prints the version of Node you have node --version # Now verify npm is installed — it comes bundled with Node # This prints your npm version npm --version # If you see version numbers printed, you're good to go. # If you see 'command not found', visit nodejs.org and install Node first.
10.2.4
Creating Your First package.json — The Project's Identity Card
Every npm project needs a package.json file sitting at its root. This file is the heart of your project. It records your project's name, version, which packages it depends on, and useful scripts for running or testing your app. Without it, npm has no idea what your project needs.
You create it by running 'npm init' inside your project folder. npm will ask you a series of questions — project name, version, description, entry point, and so on. If you just want sensible defaults and don't want to answer every question, use 'npm init -y' (the -y flag means 'yes to everything').
The resulting file is plain JSON — just text. You can open it in any code editor and edit it by hand. That's one of the things beginners find surprising: it's not magic, it's just a config file you can read and change.
Every field in package.json has a specific meaning. The two most important are 'name' (what your package is called, lowercase, no spaces) and 'version' (which follows a three-number system called Semantic Versioning, or semver, like 1.0.0). We'll look at what those numbers mean in a moment.
# Step 1: Create a new folder for your project mkdir my-first-node-project # Step 2: Move into that folder cd my-first-node-project # Step 3: Create a package.json with all default values # The -y flag skips the questionnaire and accepts all defaults npm init -y # Step 4: Look at what was created cat package.json
{
"name": "my-first-node-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Installing Packages — Dependencies vs devDependencies
Now for the fun part: pulling in other people's code. When you run 'npm install <package-name>', npm downloads that package into a folder called node_modules and automatically records it in your package.json under a field called 'dependencies'.
But not all packages are equal. Some packages your app needs to actually run in production — like express (a web server) or axios (for making HTTP requests). These go in 'dependencies'. Other packages are only needed while you're developing — like jest (a testing tool) or eslint (a code quality checker). Your end users will never need these. These go in 'devDependencies'.
To install a production dependency: 'npm install axios' To install a dev-only dependency: 'npm install jest --save-dev'
The '--save-dev' flag is the difference. Get this right and your production deployments only install what they actually need, which makes them faster and leaner.
Semantic versioning controls which version gets installed. The version number '1.4.2' means: major version 1, minor version 4, patch 2. A caret symbol (^1.4.2) means 'install 1.4.2 or any compatible minor/patch update'. A tilde (~1.4.2) means 'install 1.4.2 or any patch update only'. Most of the time you'll see the caret and that's fine for beginners.
// First, in your terminal run: // npm install axios // npm install chalk --save-dev // Now let's use axios — a package for making HTTP requests // This is a PRODUCTION dependency (your app needs it to run) const axios = require('axios'); // Fetch a public post from a free test API async function fetchBlogPost(postId) { try { // axios.get sends an HTTP GET request to the URL const response = await axios.get( `https://jsonplaceholder.typicode.com/posts/${postId}` ); // response.data contains the actual JSON returned by the API const post = response.data; console.log('Post Title:', post.title); console.log('Post Body:', post.body); } catch (error) { // If the request fails, catch and print the error console.error('Failed to fetch post:', error.message); } } // Fetch the first blog post fetchBlogPost(1);
Post Body: quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto
npm Scripts — Your Project's Command Centre
The 'scripts' field in package.json is one of the most underappreciated features for beginners. It lets you define shortcut commands for your project that anyone on the team can run without knowing the underlying tool or its exact flags.
For example, instead of everyone having to remember 'node --watch src/index.js', you define a 'start' script once and everyone just types 'npm start'. Instead of 'jest --coverage --verbose', you write a 'test' script.
There are two special script names: 'start' and 'test'. These run with 'npm start' and 'npm test'. Any other custom script name you invent runs with 'npm run <script-name>' — note the extra 'run' keyword.
Scripts can also chain commands together using '&&' (run second command only if first succeeds) or '&' (run both simultaneously). This is how teams build, lint, and test their code in one command. It's also how CI/CD pipelines (automated deployment tools) know how to build your project on a server.
Think of scripts as the user manual for your project. A new developer clones your repo, reads the scripts section, and immediately knows how to start, test, build, and deploy the app — without asking you.
{
"name": "my-first-node-project",
"version": "1.0.0",
"description": "A demo project to learn npm and package.json",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js",
"test": "jest --coverage",
"lint": "eslint src/**/*.js",
"build:and:lint": "npm run lint && node index.js"
},
"dependencies": {
"axios": "^1.6.7"
},
"devDependencies": {
"chalk": "^5.3.0",
"eslint": "^8.56.0",
"jest": "^29.7.0"
},
"keywords": ["demo", "nodejs", "npm"],
"author": "Your Name <you@example.com>",
"license": "MIT"
}
# Executes: node index.js
# Output: whatever your index.js prints
# Running: npm run dev
# Executes: node --watch index.js
# Node watches for file changes and auto-restarts — great for development
# Running: npm test
# Executes: jest --coverage
# Jest finds and runs all test files and prints a coverage report
Understanding node_modules and package-lock.json
When you run npm install, npm downloads every package and its own dependencies (sub-dependencies) into a folder called node_modules. This folder is the runtime environment for your project. It's also massive — a typical React project can have 200,000+ files in node_modules. That's why you never commit it to Git.
Instead, npm created package-lock.json. This file records the exact version of every single package and every sub-package that was installed. It's a complete snapshot of your dependency tree. When someone else clones your repo and runs npm install, npm reads the lockfile and installs the exact same versions, down to the sub-dependency. This eliminates "works on my machine" problems.
Without package-lock.json, if package 'A' depends on package 'B' with range ^1.0.0, and 'B' releases version 1.1.0 after you last ran install, your teammate gets B@1.1.0 while you have B@1.0.0. The lockfile locks everything.
You should ALWAYS commit package-lock.json to Git. Never delete it, never add it to .gitignore. Some old tutorials say to delete it because it's "magical" — that's terrible advice. Keep it.
{
"name": "my-first-node-project",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "my-first-node-project",
"dependencies": {
"axios": "^1.6.7"
}
},
"node_modules/axios": {
"version": "1.6.7",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz",
"integrity": "sha512-...",
"dependencies": {
"follow-redirects": "^1.15.4"
}
},
"node_modules/follow-redirects": {
"version": "1.15.5",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
"integrity": "sha512-..."
}
}
}
npm install to regenerate correctly.| Aspect | dependencies | devDependencies |
|---|---|---|
| Purpose | Code your app needs to actually run | Tools you need while building/testing |
| Examples | express, axios, mongoose, react | jest, eslint, typescript, webpack |
| Install command | npm install <package> | npm install <package> --save-dev |
| Installed in production? | Yes — always | No — skipped with npm install --production |
| Appears in package.json under | "dependencies": {} | "devDependencies": {} |
| Risk if missing in prod | App crashes or won't start | No impact — prod doesn't need them |
🎯 Key Takeaways
- npm is a package manager — it lets you install, share, and manage reusable code. It installs automatically with Node.js, so you don't need to set it up separately.
- package.json is your project's identity card and shopping list — it records the project name, version, scripts, and every package the project depends on.
- dependencies are packages your app needs to run in production (like express or axios). devDependencies are tools you only need while building or testing (like jest or eslint). Getting this wrong wastes resources on production servers.
- npm scripts in package.json are shortcut commands for your whole team. 'npm start' and 'npm test' are built-in shortcuts; every other custom script runs with 'npm run <script-name>'.
- Always commit package-lock.json to Git. It locks exact versions of every dependency and prevents irreproducible builds.
⚠ Common Mistakes to Avoid
Interview Questions on This Topic
- QWhat is the difference between dependencies and devDependencies in package.json, and can you give a real-world example of each?JuniorReveal
- QIf a colleague clones your Node.js repository and the node_modules folder isn't there, what command do they run and how does npm know what to install?JuniorReveal
- QWhat does the caret symbol (^) mean in a version like ^1.4.2 inside package.json, and how is that different from specifying an exact version like 1.4.2 with no symbol?Mid-levelReveal
- QExplain what happens when you run
npm installand how the lockfile ensures deterministic builds.SeniorReveal
Frequently Asked Questions
What is the difference between npm and Node.js?
Node.js is a runtime — it's the engine that lets you run JavaScript outside of a browser. npm is a package manager that comes bundled with Node.js. Think of Node.js as a car engine and npm as the mechanic's tool kit — one runs your code, the other helps you manage the parts you need to build it.
What is package-lock.json and do I need it?
package-lock.json is an automatically generated file that records the exact version of every package and sub-package installed in your project. You should always commit it to Git. It ensures that every developer on your team and every production server installs the exact same versions, preventing subtle 'works on my machine' bugs.
Can I edit package.json by hand or should I only use npm commands?
You can absolutely edit package.json by hand — it's just a plain text JSON file. Developers do it all the time to update scripts, add metadata, or tweak version ranges. After editing it manually, run 'npm install' to make sure your node_modules folder reflects the changes you made.
Why should I not use sudo with npm?
Using sudo changes ownership of installed files to root, which causes permission errors for other users and can break global commands like create-react-app. Instead, configure npm to use a user-level directory by running npm config set prefix ~/.npm-global and adding that to your PATH. Better yet, use nvm to manage Node versions without any permission issues.
What's the difference between npm and npx?
npm installs packages globally or locally and adds them to your project. npx executes packages without installing them permanently — ideal for running one-off tools like create-react-app without cluttering global space. For example, npx create-react-app my-app downloads and runs the latest version without leaving a permanent install.
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.