Photo by Ferenc Almasi on Unsplash
So there I was, staring at my terminal at like 11pm, trying to get a new project set up before a morning standup. I ran npm install and immediately got hit with this:
npm warn saveError ENOENT: no such file or directory, open '/Users/mike/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/Users/mike/package.json'
npm WARN mike No description
npm WARN mike No repository field.
npm WARN mike No README data
npm WARN mike No license field.
Classic. The wrong directory npm install error. It's one of those things that feels embarrassing to troubleshoot, but honestly? I've seen this trip up developers at every level — including people who've been doing this for years. You get in a rhythm, you're copying commands from a README, and suddenly npm is trying to install packages in your home directory or some random parent folder instead of your actual project.
Let me break down what's actually happening and the fastest ways out of it.
Why This Happens
npm install looks for a package.json file in the current working directory. If it doesn't find one, it doesn't throw a hard stop — it just sort of shrugs and starts creating a new phantom project right where you're standing. That ENOENT error is Node's way of saying "I looked for this file and it simply doesn't exist here."
The usual culprits? You cloned a repo but forgot to cd into it. Or maybe you opened a new terminal tab and it defaulted back to your home directory. Sometimes it's a nested project structure where the package.json lives a level deeper than you expected. I've also seen monorepos cause total confusion here — you think you're in the right workspace folder, but you're actually one level too high.
Fix #1: Just cd Into the Right Place (Obviously, But...)
First thing — confirm where you actually are and where your package.json lives.
pwd
That'll print your current directory. Then check if package.json is there:
ls package.json
If you get another ENOENT, it's not there. Find it:
find . -name "package.json" -not -path "*/node_modules/*"
That -not -path "*/node_modules/*" part is important — without it you'll get flooded with results from inside node_modules. Once you spot the right path, just navigate there:
cd your-project-folder
npm install
Simple, but you'd be surprised how often this is all you need.
Fix #2: Use the --prefix Flag to Point npm at the Right Directory
Here's something a lot of people don't know — you can actually tell npm exactly which directory to use without navigating there first. The --prefix flag lets you specify the path directly:
npm install --prefix /full/path/to/your/project
Or if you're close by, a relative path works fine:
npm install --prefix ./my-project
This is genuinely useful when you're running npm commands from a script or a CI/CD pipeline where you can't always control which directory you're in at execution time. I've used this in Makefiles and shell scripts more times than I can count. It keeps things explicit, which is always nice.
Fix #3: If package.json Is Actually Missing, Initialize It
Now here's where it gets tricky — sometimes there's no wrong directory situation at all. Sometimes package.json just... doesn't exist yet. Maybe someone set up the repo without committing it, or you're starting fresh and skipped the init step.
In that case, stop trying to install first. Create the package.json:
npm init -y
The -y flag auto-accepts all the defaults so you're not sitting there hitting Enter twelve times. Once that file exists, npm install will work as expected. Then add whatever dependencies you actually need:
npm install express dotenv
(Side note — if you're working from someone else's repo and their package.json wasn't committed, that's a whole separate conversation about git hygiene. Gently remind them to not put package.json in their .gitignore. I've seen it happen.)
Fallback Tip: Alias Your Project Directories
If you constantly find yourself running npm commands in the wrong place because you have a dozen projects open across different terminal sessions, do yourself a favor and set up some shell aliases or use a tool like direnv. With direnv, you can define environment-specific behavior per directory — so the moment you cd into a project, your shell knows exactly what context it's in.
At the very least, just get in the habit of running this before any npm command:
ls package.json && npm install || echo "No package.json found — check your directory"
It's a tiny sanity check that takes zero extra effort once it's in your muscle memory. Feels a little overkill until the one time it saves you from accidentally dumping node_modules into your home folder. Trust me on this one.
The wrong directory npm install error is almost never serious, but it can eat up way more time than it should when you're already in a hurry. Hope this gets you unstuck quickly.
Related: How to Fix npm Error Code ENOENT (And Why It Keeps Happening)
๋๊ธ
๋๊ธ ์ฐ๊ธฐ