Last updated: Feb 24, 2023
Reading timeยท3 min
The error "npm ERR! code ENOENT syscall open" occurs when you issue an npm
command outside of the root directory of your project.
To solve the error, cd
into the root directory of your project before
issuing the command or generate a new package.json
file in the directory.
npm ERR! code ENOENT npm WARN saveError ENOENT: no such file or directory npm ERR! syscall open npm ERR! path C:\Users\Public\bobbyhadz\package.json npm ERR! errno -4058 npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\Public\bobbyhadz\package.json' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\bobbyhadz\AppData\Local\npm-cache\_logs\2022-11-29T18_42_26_275Z-debug-0.log
The most common cause of the error is using the npm install
command without
having your terminal open in the root directory of your project (where your
package.json
file is).
The second most common cause of the error is having a glitched node_modules
directory (in which case you have to delete node_modules and rerun
npm install
).
Forgetting to initialize a package.json
file before running the
npm install
command.
If you got the error "ENOENT, no such file or directory", you likely forgot
to initialize a package.json
file in the root directory of your project.
You can use the npm init -y
command to create a package.json
file.
npm init -y
The npm init -y command
creates a package.json
file in the current directory.
The package.json
file is used to track the installed NPM packages and their
versions.
You can use the npm install
command after generating a package.json
file.
npm install axios
Make sure to replace axios
with the name of the package you're trying to
install.
npm install
without being in the root directoryMake sure to cd
into the root directory of your project before issuing the
npm install
command.
Desktop <-- You are probably here |-- my_project <-- You have to be here |-- package.json
Assuming that your project's directory is named my_project
, issue the
following commands.
cd my_project npm install
If your project doesn't have a package.json
file, you have to create one with
the following command.
npm init -y
Once you create a package.json
file, you can install modules.
npm install express
Use the following command to make sure there is a package.json
file in the
directory your shell is currently located in before issuing npm
commands.
# ๐๏ธ if you are on Windows dir # ๐๏ธ if you are on macOS/Linux ls
If there isn't a package.json
file in the directory, you either have to cd
into your project's root directory or use the npm init -y
command to generate
a package.json
file.
On Windows you can open PowerShell in a specific folder:
Shift
and right-click in Explorer.npm install
command.If you are sure that your terminal is located in your project's root directory
(where your package.json
file is), delete your node_modules
and
package-lock.json files and rerun
npm install
.
# ๐๏ธ on Windows - delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json # ๐๏ธ on macOS/Linux - delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # ๐๏ธ clean npm cache npm cache clean --force # ๐๏ธ install packages npm install
Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and needs a reboot.