Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
brianpmaher opened this issue Nov 4, 2024 · 9 comments
Open
2 tasks done

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

brianpmaher opened this issue Nov 4, 2024 · 9 comments
Labels
Bug thing that needs fixing Needs Triage needs review for next steps

Comments

@brianpmaher
Copy link

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
@brianpmaher brianpmaher added Bug thing that needs fixing Needs Triage needs review for next steps labels Nov 4, 2024
@Kyle-Ignis
Copy link

Agreed. Please fix.

@kchindam-infy
Copy link

@brianpmaher I have tested the scenario where running npm install in an empty directory does not create a package.json or any other files, which seems to align with expected behavior npm generally needs a package.json to install dependencies.

@Kyle-Ignis
Copy link

Screenshot 2024-11-08 at 11 51 37 AM

i dont believe this what we expect to happen

@brianpmaher
Copy link
Author

I misspoke, it creates a package-lock.json, not package.json and it does also create a directory when using --prefix

@pat-in-a-hat
Copy link

pat-in-a-hat commented Nov 8, 2024

a similarly frustrating experience is if you npm uninstall a package in the wrong directory. npm will also go ahead and build a package-lock.json and an empty package.json.

desired behavior would be for npm to warn you that no package.json file was found in that directory

@pat-in-a-hat
Copy link

this appears to be easily reproducible.

Steps To Reproduce

  1. Create a new directory mkdir temp
  2. Enter the directory cd temp
  3. npm uninstall is-even

A package.json and package-lock.json file will now be present

Expected Behavior

When a user runs step 3 npm should warn that no package.json file is present then end

@Kyle-Ignis
Copy link

Screenshot 2024-11-08 at 12 28 00 PM

@Kyle-Ignis
Copy link

I would be happy to author a PR on this, especially if someone is willing to point me in the right direction just to get started.

@kchindam-infy
Copy link

@Kyle-Ignis Appreciate if you can raise the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps
Projects
None yet
Development

No branches or pull requests

4 participants