执行npm install时提示当前目录无package.json但文件实际存在的问题求助
npm install Can't Find package.json (Even Though It Exists) Let's break down this annoying problem—you've got a valid package.json right in your project folder, but npm install keeps throwing an error claiming it can't find it. Looking at your terminal output, there's a key clue we can use to debug this.
Your Terminal Output for Reference
myapp$ pwd /code/myapp myapp$ npm --version 5.8.0 myapp$ nvm --version 0.33.5 myapp$ pip --version pip 10.0.0 from /Library/Python/2.7/site-packages/pip (python 2.7) myapp$ node --version v8.11.1 myapp$ ls package.json package.json myapp$ npm install npm ERR! code ENOPACKAGEJSON npm ERR! package.json Non-registry package missing package.json: <REDACTED>. npm ERR! A complete log of this run can be found in: npm ERR! /Users/[myuser]/.npm/_logs/202X-XX-XXTXX-XX-XXZ-debug.log
Most Likely Fix: Check for Invalid Local Dependencies
The line Non-registry package missing package.json: <REDACTED> is the biggest hint here. This almost always means your package.json has a dependency pointing to a local directory (using the file: prefix, like "my-local-package": "file:./../my-other-project") and either:
- That local directory no longer exists, or
- The directory exists but doesn't have its own
package.jsonfile.
How to fix it:
- Open your
package.jsonfile and scan thedependenciesanddevDependenciessections. - Look for any entries starting with
file:. - Verify the path is correct—make sure the target folder exists and has a valid
package.json. - If you don't need that local dependency anymore, just delete the entry from your
package.json.
Other Possible Fixes to Try
If the local dependency check doesn't solve it, work through these steps one by one:
1. Clear the npm Cache
Corrupted cache data can cause weird behavior even when your files are perfectly fine. Wipe the cache with:
npm cache clean --force
Then re-run npm install.
2. Check File Permissions
It's possible npm doesn't have permission to read your package.json or project folder. Check permissions with:
ls -l package.json ls -ld /code/myapp
Ensure your user account has read (and write, for generating lock files) access. If not, adjust permissions:
chmod 644 package.json chmod 755 /code/myapp
3. Refresh Your NVM Environment
Since you're using nvm, switching Node versions can sometimes leave behind odd state. Reinstall your current Node version to reset npm:
nvm reinstall v8.11.1
Then try npm install again.
4. Validate Your package.json Syntax
Even if the file exists, invalid JSON (like missing commas or mismatched brackets) can make npm fail to parse it. Validate it with this quick command:
cat package.json | python -m json.tool
If there's a syntax error, this will throw an error and point you to the problem spot.
内容的提问来源于stack exchange,提问作者shravanp

