npm / npm/cli

[BUG] npm install creates directories and empty package.json

Open
#7,892 9 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Needs Triage
Dominant language
JavaScript
Stars
10.1k
Forks
4.7k
Avg merge
2d 2h
Merged PRs (30d)
19

Description

Is there an existing issue for this?
  • I have searched the existing issues
This issue exists in the latest npm version
  • I am using the latest npm
Current Behavior

Running npm install from a directory without a package.json creates a empty package.json file.
Additionally, running npm --prefix some-directory install will also create the directory.

Expected Behavior

100% of the time I have done this, it has been on accident and I then have to go and delete the directory and/or the package.json file it creates.

I think it would be better to add an error message output indicating that there is no package.json present in the directory or the directory doesn't exist, and then suggesting I run npm init instead.

Steps To Reproduce
  1. Create a new temporary directory wherever mkdir temp
  2. Enter the directory cd temp
  3. npm --prefix temp2 install
Environment
  • npm: 10.8.3
  • Node.js: 22.9.0
  • OS Name: MacOS Sonoma 14.6.1
  • System Model Name: Apple M3 Pro

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing npm install in a directory without package.json and npm --prefix temp2 install when the target directory is absent. Compare the created files and directories with the expected error behavior described in the issue. Done means both commands avoid unintended filesystem changes and provide an actionable error suggesting npm init.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.