Nest.js出现ENOENT错误:找不到文件或目录,无法打开'./push-service-account.json'的解决方案咨询
Hey there, I get that dealing with ENOENT errors when your app can't find a critical file is super frustrating—especially when you can't just delete package-lock.json because you rely on npm ci. Let's walk through targeted solutions that fit your Node 12.22.12 / npm 6.14.16 setup:
Double-check the file path and location
First, make 100% surepush-service-account.jsonactually exists where your code is looking for it. The error points to./push-service-account.json, which means it expects the file in the current working directory where you launch your Nest.js app.- If you run the app from a subfolder (like
src/instead of the project root), the relative path breaks. Either run the app from the project root, or adjust the path in your code to be absolute using Node'spathmodule:path.resolve(__dirname, '../push-service-account.json')(where__dirnamerefers to the current file's directory). - Verify the filename is spelled perfectly—case sensitivity matters on Linux/macOS, so
Push-Service-Account.jsonwon't match the lowercase path in your error.
- If you run the app from a subfolder (like
Ensure the file isn't gitignored or missing from your environment
Sometimes service account files get accidentally added to.gitignore. Check your.gitignoreto confirmpush-service-account.jsonisn't listed. If it was ignored, either add it to your repo (if safe) or manually copy it to your development/deployment environment.- For CI/CD pipelines using
npm ci, make sure the file is either included in the repo or injected as a secure asset during the pipeline—npm cionly manages dependencies, not project-specific files like this.
- For CI/CD pipelines using
Check your npm script's working directory
If you start your app via an npm script (e.g.,npm run start), the working directory might not be what you expect. For example, a script likecd src && nest startwill make your app look for the JSON file insrc/instead of the project root.- Edit your
package.jsonscripts to run the app from the correct directory, or switch to absolute paths in your code as mentioned earlier.
- Edit your
Verify file permissions (Linux/macOS)
On Unix-based systems, even if the file exists, your Node process might lack read access. Runls -l push-service-account.jsonin your project root to check permissions. If needed, runchmod 644 push-service-account.jsonto grant read access to the file.Clear the npm cache (no impact on package-lock.json)
Don't worry—clearing the npm cache won't touch yourpackage-lock.json, and sometimes cached files can cause unexpected path quirks. Runnpm cache clean --force, then re-runnpm cito ensure dependencies are installed correctly. Sincenpm cistrictly follows your lock file, this is totally safe.
These steps should cover the most common causes without requiring you to delete your package-lock.json. Remember, npm ci only manages dependencies—it doesn't handle missing project assets like your service account file, so the fix almost always boils down to ensuring the file is in the right place and your code references it correctly.
内容的提问来源于stack exchange,提问作者Aleksandr Semenov

