npm: npm can install packages in the parent folder

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


I’ve had some students of my bootcamp run into this problem I never noticed.

It’s due to the behavior of npm when installing a package in an empty folder.

I was suggesting to use npm install <packagename, for example in this way:

npm install my-prime

in an empty folder.

By default this creates a package.json with the package as a dependency, a package-lock.json and installs the package in node_modules.

But some people were not seeing this happen. Nothing seemed to happen.

What happened however is that they had a package.json file and a node_modules folder up in the directory tree.

Maybe not even in the parent folder, but higher in the hierarchy.

Maybe they ran npm install <package> in their home folder without realizing, probably for a test.

npm will walk up the folder tree checking for a folder that contains either a package.json file, or a node_modules folder. If such a thing is found, then that is treated as the effective “current directory” for the purpose of running npm commands.

Source

To fix this problem, the best solution is to remove the parent package.json and node_modules.

It’s probably there by mistake.

Otherwise you could also run npm init -y in the folder to create a blank package.json file, then re-run the npm install <package> command, which will now work as expected.

Lessons in this unit:

0: Introduction
1: How to use or execute a package installed using npm
2: npm dependencies and devDependencies
3: How to fix the "Missing write access" error when using npm
4: ▶︎ npm can install packages in the parent folder
5: Install an older version of an npm package
6: Find the installed version of an npm package
7: How to test an npm package locally
8: npm global or local packages
9: What are peer dependencies in a Node module?
10: `npm run dev` is a long-running program
11: Semantic Versioning using npm
12: Uninstalling npm packages with `npm uninstall`
13: An introduction to the npm package manager
14: The npx Node Package Runner
15: The package.json guide
16: The package-lock.json file
17: What is pnpm?
18: Should you commit the node_modules folder to Git?
19: Update all the Node dependencies to their latest version
20: Where does npm install the packages?
21: Bumping Node.js dependencies
22: Run package.json scripts upon any file changes in a folder