npm ERR! code ENOENT syscall open error [Solved]

avatar
Borislav Hadzhiev

Last updated: Feb 24, 2023
3 min

banner

# npm ERR! code ENOENT syscall open error [Solved]

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

shell
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

# Common causes of the error

  1. 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).

  2. 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).

  3. Forgetting to initialize a package.json file before running the npm install command.

# ENOENT, no such file or directory

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.

shell
npm init -y

create package json file

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.

shell
npm install axios

Make sure to replace axios with the name of the package you're trying to install.

# Using npm install without being in the root directory

Make sure to cd into the root directory of your project before issuing the npm install command.

shell
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.

shell
cd my_project npm install

cd into correct directory

If your project doesn't have a package.json file, you have to create one with the following command.

shell
npm init -y

Once you create a package.json file, you can install modules.

shell
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.

shell
# ๐Ÿ‘‡๏ธ if you are on Windows dir # ๐Ÿ‘‡๏ธ if you are on macOS/Linux ls

make sure package json exists

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:

  1. Open your project's root directory in a window.
  2. Press Shift and right-click in Explorer.

windows open powershell window here

  1. Click on "Open PowerShell window here".
  2. Run the npm install command.

# Delete your node_modules and reinstall your dependencies

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.

shell
# ๐Ÿ‘‡๏ธ 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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev