Ошибка enoent no such file or directory

I have a Node.js web application currently running on a server successfully. Now I’m trying to set up a local copy on my development server.

I currently have Node.js, NPM and MongoDB Installed just like what I have in production server. However, the error below occurs when I try to start the Node.js server.

What could be causing this issue?

cd ~/node/nodeapp
node app.js

Output:

fs.js:640
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open '/home/embah/node/nodeapp/config/c
onfig.json'
    at Error (native)
    at Object.fs.openSync (fs.js:640:18)
    at Object.fs.readFileSync (fs.js:508:33)
    at Object.<anonymous> (/home/embah/node/nodeapp/config/config.js:4:28)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/embah/node/glorby/app.js:13:16)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

Peter Mortensen's user avatar

asked Apr 6, 2017 at 16:19

Emeka Mbah's user avatar

1

Your app is expecting to find a file at /home/embah/node/nodeapp/config/config.json but that file does not exist (which is what ENOENT means). So you either need to create the expected directory structure or else configure your application such that it looks in the correct directory for config.json.

answered Apr 6, 2017 at 16:23

Trott's user avatar

TrottTrott

65.7k23 gold badges171 silver badges211 bronze badges

0

After going through so many links and threads and getting frustrated over and over again, I went to the basics and boom! it helped. I simply did:

npm install

I don’t know, but it might help someone :)

answered Jun 16, 2018 at 21:15

olleh's user avatar

olleholleh

1,86518 silver badges21 bronze badges

5

Regarding:

92% additional asset processing scripts-webpack-plugin× 「wdm」: Error: ENOENT: no such file or directory, open….

If anyone faced to such an error, you should do followings:

  1. you should check if the file path is correct in the angular.json file.

    «scripts»: [
    «node_modules/jquery/dist/jquery.min.js»,
    «node_modules/bootstrap/dist/js/bootstrap.js»
    ],

  2. you should press Ctrl + C and rerun the project.

Peter Mortensen's user avatar

answered Jun 27, 2019 at 16:10

Odiljon Djamalov's user avatar

1

olleh’s answer worked, because npm install will create a node_modules directory in the current path where it is executed. So, while using the file server system module, the below declaration locate files from the top level directory of node_modules.

const fs = require('fs')

Peter Mortensen's user avatar

answered May 7, 2019 at 23:27

Aniruddha Pandey's user avatar

In my case the issue was caused by using a file path starting at the directory where the script was executing rather than at the root of the project.

My directory stucture was like this:
projectfolder/
├── package.json
├── scriptFolder/
│ ├── myScript.js

And I was calling fs.createReadStream('users.csv') instead of the correct fs.createReadStream('scriptFolder/users.csv')

answered Jun 15, 2020 at 22:55

Chase Denecke's user avatar

0

If you have the same error while using Express.js in your project this worked for me:

In my project, when I ran an Express.js application, I noticed that the current working directory was the root directory of the project (while I was trying to read a file that was located in the script’s directory).
It could not run the file since process.cwd() !== __dirname.

You can check it out and console log process.cwd() in the script you are trying to read the JSON file with.

I just changed the path to:

const fs = require('fs');
fs.readFileSync(`${__dirname}\FILENAME`);

Peter Mortensen's user avatar

answered Jan 29, 2022 at 20:20

Niv's user avatar

NivNiv

4951 gold badge7 silver badges17 bronze badges

I also had this issue, because I had another console window open that was running the application and I was attempting to rerun yarn start in another console window.

The first Yarn executing prevented the second from writing. So I just killed the first process and it worked.

Peter Mortensen's user avatar

answered May 15, 2018 at 8:24

Damian Green's user avatar

Damian GreenDamian Green

6,7862 gold badges31 silver badges43 bronze badges

1

I was facing this issue with ng-package.json file, while creating a plugin. I found out I was providing the wrong path in angular.json. Fixed my file path, issue was resolved.

May be helpful for someone.

answered Mar 24, 2021 at 10:06

Analyst's user avatar

AnalystAnalyst

7416 silver badges15 bronze badges

In my case, the issue occurred after I switched from a Git branch to another, references to some old files remained in the scripts inside the «node_modules/.cache» directory.

Deleting the «node_modules», «build» directories and «package-lock.json» file and then issuing the «npm install» command has fixed the issue for me.

Peter Mortensen's user avatar

answered May 18, 2020 at 12:29

Hasnaa Ibraheem's user avatar

Hasnaa IbraheemHasnaa Ibraheem

1,0921 gold badge10 silver badges16 bronze badges

1

I solved this error by simply creating a blank file at that location for which I got the error. If you are getting the error for a directory, you can try by also creating an empty directory.

Peter Mortensen's user avatar

answered Jun 13, 2020 at 17:58

sanghamitra Ravi Kant's user avatar

0

If there isn’t any layout folder, just use it like this in your routing:

app.get('/', (req, res) => {
    res.render('something', { layout: false });
})

Here change something with your folder name.

Peter Mortensen's user avatar

answered Jun 27, 2020 at 6:10

MD.FAHIM HOSSEN's user avatar

0

Make sure your angular.json file has the «style» and «scripts» array as below (for Angular 12 and above):

"styles": [
          "src/styles.css",
          "./node_modules/bootstrap/dist/css/bootstrap.css"
        ],
"scripts": [
          "node_modules/jquery/dist/jquery.min.js",
          "node_modules/bootstrap/dist/js/bootstrap.js"
        ]

Once this is done, press Ctrl + C and then ng serve.

Peter Mortensen's user avatar

answered Jun 24, 2021 at 5:17

Ratnesh Rai's user avatar

I don’t know if anyone would see this, but I’ll say my answer.

First you need to be in the app directory that you created with the following command:

npx create-react-app [app-name]

Next run:

sudo npm install

to make it install all dependencies from

package.json

Then run:

sudo npm start

and make sure to run them with the sudo command, because sometimes it is absolutely necessary.

Peter Mortensen's user avatar

answered Dec 5, 2021 at 18:23

Javad's user avatar

JavadJavad

2032 silver badges9 bronze badges

1

I added the PM2_HOME environment variable on the system level and now it works all right.

Peter Mortensen's user avatar

answered Nov 2, 2020 at 15:27

kepy97's user avatar

kepy97kepy97

96810 silver badges12 bronze badges

1

If you’re coding with TypeScript, remember that the transpiled-JS folder is where JavaScript would be searching for your file and will definitely not be able to find your html-file; hence such error.

Therefore, you’d need to copy the file into the transpiledJS folder for it to be located.

Peter Mortensen's user avatar

answered Nov 15, 2021 at 13:30

Chukwunazaekpere's user avatar

Sometimes you see this issue in the following scenario.

Let’s assume that you have a folder structure like node-projects (folder) → my-blog (folder) → my-blog (folder where the actual project exists), but you are in the my-blog directly, and it is an immediate child of node-project. Try to run your command in that directory.

As the project is present in my-blog that is an immediate child of my-blog. So, in this case, it searches your config.json file in my-blog (which is an immediate child of node-projects) rather than finding the config.json file in my-blog that is an immediate child of my-blog.

Just transfer your directory by hitting the command cd my-blog in your terminal and that’s it. The problem is resolved!

Peter Mortensen's user avatar

answered Oct 18, 2022 at 21:27

HamzaKhalid273's user avatar

HamzaKhalid273HamzaKhalid273

3271 gold badge3 silver badges11 bronze badges

In my case

import { Object } from '../config/env';

gave me the error.

I solved it with change the address like this:

import { Object } from './../config/env';

answered Apr 24, 2019 at 13:40

msalihbindak's user avatar

Weirdly, in my project, I always get this error first time I add/remove a package, but then I run the same command again and it works on the second run.

answered Apr 25, 2022 at 14:59

Michal Kurz's user avatar

Michal KurzMichal Kurz

1,50012 silver badges38 bronze badges

My mistake was not adding / before the path. Correct path: /Users/mee/Documents/file_name.jpg

answered Oct 11, 2022 at 4:58

GorvGoyl's user avatar

GorvGoylGorvGoyl

41.1k28 gold badges224 silver badges218 bronze badges

Running

npm run scss
npm run start 

in that order in the terminal solved the issue for me.

Peter Mortensen's user avatar

answered Mar 11, 2020 at 11:44

Onat Korucu's user avatar

Onat KorucuOnat Korucu

94211 silver badges13 bronze badges

1

I had this error while uninstalling Bootstrap from the project, so I just deleted the script and style from file angular.json.

"styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css"
            ],
 "scripts": [
              "node_modules/jquery/dist/jquery.min.js"
          ]

Cleared this to

"styles"  : [    ],
"scripts" : [    ]

This solved my issue.

Peter Mortensen's user avatar

answered Jul 19, 2022 at 6:38

saurabh solanke's user avatar

I work with Visual Studio Code, Git and Nx and often have the problem that Visual Studio Code performs an auto staging.

In combination with Nx, it comes very often to problems. Simply unstaging the files in Visual Studio Code often helps.

answered Feb 25, 2022 at 12:36

TimJ0212's user avatar

TimJ0212TimJ0212

111 silver badge2 bronze badges

1

It’s happened with me. I deleted some CSS files by mistake and then copied back. This error appeared at that time. So I restarted all my Docker images and other servers and then it went away.

Peter Mortensen's user avatar

answered Apr 3, 2019 at 11:35

Yasir Akbar's user avatar

I tried something and got this error:

Error: ENOENT: no such file or directory, open ‘D:WebsiteNodemailerNodemailer-applicationviewslayoutsmain.handlebars’

The fix I got was to literally construct the directory as it is seen. This means labeling the items exactly as shown. It is weird that I gave the computer what it wanted.

Peter Mortensen's user avatar

answered May 25, 2020 at 19:53

Blank456's user avatar

1

Update:

I think in my case it happened because the project was initially installed with pnpm, and I ran yarn add thereafter.


Hitting this error consistently today.

Both locally on MacOS and on Ubuntu in CI: https://github.com/akd-io/create-next-stack/actions/runs/4772052085/jobs/8484361470?pr=172

local yarn-error.log:

Arguments: 
  /Users/akd/.nvm/versions/node/v18.13.0/bin/node /Users/akd/.nvm/versions/node/v18.13.0/bin/yarn add @emotion/react@^11.0.0 @emotion/styled@^11.0.0 @chakra-ui/icons@^2.0.0 @chakra-ui/react@^2.0.0 @mui/material@^5.0.0 react-hook-form@^7.0.0 formik@^2.0.0 framer-motion@^9.0.0 mrm@^4.0.0 mrm-task-lint-staged@^7.0.0

PATH: 
  /Users/akd/workspace/create-next-stack/packages/create-next-stack/node_modules/.bin:/Users/akd/Library/pnpm/global/5/.pnpm/pnpm@8.3.0/node_modules/pnpm/dist/node-gyp-bin:/Users/akd/workspace/create-next-stack/node_modules/.bin:/Users/akd/Library/pnpm:/opt/homebrew/opt/openjdk/bin:/Users/akd/.nvm/versions/node/v18.13.0/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/akd/Library/pnpm:/opt/homebrew/opt/openjdk/bin:/Users/akd/.nvm/versions/node/v18.13.0/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/akd/.yarn/bin:/Users/akd/.yarn/bin:/Users/akd/.nvm/versions/node/v18.13.0/bin

Yarn version: 
  1.22.19

Node version: 
  18.13.0

Platform: 
  darwin arm64

Trace: 
  Error: ENOENT: no such file or directory, copyfile '/Users/akd/Library/Caches/Yarn/v6/npm-globby-11.1.0-bd4be98bb042f83d796f7e3811991fbe82a0d34b-integrity/node_modules/globby/index.js' -> '/Users/akd/workspace/create-next-stack-tests/run-d4eb6dcc-9846-4222-a5ef-100d98053b1e/node_modules/@typescript-eslint/typescript-estree/node_modules/globby/index.js'

npm manifest: 
  {
    "name": "run-d4eb6dcc-9846-4222-a5ef-100d98053b1e",
    "version": "0.1.0",
    "private": true,
    "scripts": {
      "dev": "next dev",
      "build": "next build",
      "start": "next start",
      "lint": "next lint"
    },
    "dependencies": {
      "@types/node": "18.15.13",
      "@types/react": "18.0.38",
      "@types/react-dom": "18.0.11",
      "eslint": "8.39.0",
      "eslint-config-next": "13.3.1",
      "next": "13.3.1",
      "react": "18.2.0",
      "react-dom": "18.2.0",
      "typescript": "5.0.4"
    }
  }

yarn manifest: 
  No manifest

Lockfile: 
  No lockfile

Solution 1:[1]

Your app is expecting to find a file at /home/embah/node/nodeapp/config/config.json but that file does not exist (which is what ENOENT means). So you either need to create the expected directory structure or else configure your application such that it looks in the correct directory for config.json.

Solution 2:[2]

After going through so many links and threads and getting frustrated over and over again, I went to the basics and boom! it helped. I simply did:

npm install

I don’t know, but it might help someone :)

Solution 3:[3]

92% additional asset processing scripts-webpack-plugin× ?wdm?: Error: ENOENT: no such file or directory, open….==> if anyone faced to such error, you should do followings:
1) you should check the if the file path is correct in angular.json file.

 "scripts": [
          "node_modules/jquery/dist/jquery.min.js",
          "node_modules/bootstrap/dist/js/bootstrap.js"
        ],

2) you should press crtl+c and re run the project.

Solution 4:[4]

@olleh answer worked because npm install will create a node_modules directory in the current path where it is executed. So, while using the file server system module, the below declaration locate files from the top level directory of node_modules.

const fs = require('fs')

Solution 5:[5]

In my case the issue was caused by using a file path starting at the directory where the script was executing rather than at the root of the project.

My directory stucture was like this:
projectfolder/
??? package.json
??? scriptFolder/
? ??? myScript.js

And I was calling fs.createReadStream('users.csv') instead of the correct fs.createReadStream('scriptFolder/users.csv')

Solution 6:[6]

I also had this issue because I had another console window open that was running the app and I was attempting to re-run yarn start in another console window.

The first yarn executing prevented the second from writing. So I just killed the first process and it worked

Solution 7:[7]

If there is no layout folder.Just use like this to your routing:

app.get('/', (req, res) => {
    res.render('something', { layout: false });
})

Here change something with your folder name and Hope it will fix your error.

Solution 8:[8]

In my case, the issue occurred after I switched from a git branch to another, references to some old files remained in the scripts inside «node_modules/.cache» directory.
Deleting «node_modules», «build» directories and «package-lock.json» file then issuing «npm install» command has fixed the issue for me

Solution 9:[9]

I was facing this issue with ng-package.json file, while creating a plugin. I found out I was providing the wrong path in angular.json. Fixed my file path, issue was resolved.

May be helpful for someone.

Solution 10:[10]

i don’t know if anyone would see this but i’ll say my answer
First you need to be in the app directory that you created with following command:

npx create-react-app [app-name]

next run :

sudo npm install

to make it install all dependencies from

package.json

then run :

sudo npm start

and make sure to run them with sudo command because sometimes it is absolutely necessary.

Solution 11:[11]

I solved this error by simply creating a blank file at that location for which I got the error. If you are getting the error for a directory, You can try by creating empty directory also. All the best.

Solution 12:[12]

I added PM2_HOME environment variable on a system level and now it works alright.

Solution 13:[13]

Make sure your angular.json file has the «style» and «scripts» array as below (For Angular 12 and above):

"styles": [
          "src/styles.css",
          "./node_modules/bootstrap/dist/css/bootstrap.css"
        ],
"scripts": [              
          "node_modules/jquery/dist/jquery.min.js",
          "node_modules/bootstrap/dist/js/bootstrap.js"
        ]

Once this is done, Press CTRL + C and then ng serve

Solution 14:[14]

Running

npm run scss
npm run start 

in that order in terminal solved the issue for me.

Solution 15:[15]

Solution 16:[16]

If you’re coding with typescript, remember that the transpiled-JS folder is where JS would be searching for your file and will definitely not be able to find your html-file; hence such error. Therefore, you’d need to copy the file into the transpiledJS folder for it to be located.

Solution 17:[17]

If you have the same error while using expres.js in your project this worked for me:

In my project when I ran an express app I noticed that the current working directory was the root directory of the project (while I was trying to read a file that was located in the script’s directory).
It could not run the file since process.cwd() !== __dirname.

You can check it out and console log process.cwd() in the script you are trying to read the json file.

I just changed the the path to:

const fs = require('fs');
fs.readFileSync(`${__dirname}\FILENAME`);

Solution 18:[18]

I had a node version mismatch, installing the right version of node via nvm worked

Solution 19:[19]

I work with vsc, git and nrwl and often have the problem that vsc performs an auto staging. In combination with nrwl it comes very often to problems. Simply unstaging the files in vsc often helps.

Solution 20:[20]

Weirdly, in my project, I always get this error first time I add/remove a package, but then I run the same command again and it works on the second run.

Solution 21:[21]

Its happened with me. I deletes some css files by mistake and then copied back. This error appeared at that time. So i restart all my dockers and other servers and then it went away, Perhaps this help some one :)

Solution 22:[22]

In my case

import { Object } from '../config/env';

gave me the error.

I solved it with change the address like this:

import { Object } from './../config/env';

Solution 23:[23]

I tried something and got this error Error: ENOENT: no such file or directory, open ‘D:WebsiteNodemailerNodemailer-applicationviewslayoutsmain.handlebars’

The fix I got was to literally construct the directory as it is seen. This means labeling the items exactly as shown, It is weird that I gave the computer what it wants.

In this tutorial, we will help you to resolve the Error: ENOENT: no such file or directory, open
'student.json'
issue.

Node.js Tutorial :

  1. Install Node.js on Windows

  2. Node.js Chalk Color Example
  3. Node.js — Read command line arguments
  4. Node.js Read Write File Example
  5. Node.js Error: ENOENT: no such file or directory

Q: What is fs module in Nodejs?
Ans:

Node.js provides an inbuilt component called FS (File System). The Node. js file system module enables
us to interact with our computer’s file system.

Error: ENOENT: no such file or directory

Error: ENOENT: no such file or directory, open 'student.json'
    at Object.openSync (node:fs:585:3)
    at Object.readFileSync (node:fs:453:35)
    at Object.writeStudentData (D:nodejsfile-examplestudent-service-impl.js:24:21)
    at Object.handler (D:nodejsfile-examplestudent-app.js:10:17)
    at D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:8891
    at j (D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:4956)
    at M.handleValidationAndGetResult (D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:8860)
    at M.applyMiddlewareAndGetResult (D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:9502)
    at M.runCommand (D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:7231)
    at Xt.[runYargsParserAndExecuteCommands] (D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:57762) {
  errno: -4058,
  syscall: 'open',
  code: 'ENOENT',

Follow below steps to resolve Error: ENOENT: no such file or directory, open 'student.json'
issue.

  1. This problem occurs when a file does not exist; therefore, check to see if the file exists first.
  2. The suitable approach is to use fs.openSync, it
    returns the file descriptor.
  3. If the file does not exist, the w flag ensures that it is created, and if it does, it
    is overwritten
    with a new file, overriding its content.
  4. If users don’t need the file descriptor, users can close the file by wrapping the call in a
    fs.closeSync() call.
//check if file exist
if (!fs.existsSync('student.json')) {
    //create new file if not exist
    fs.closeSync(fs.openSync('student.json', 'w'));
}

Recommendation for Top Popular Post :

Web development has come a long way now. Instead of using simple HTML/CSS, Javascript and PHP to build websites, developers now can choose from plenty of web development frameworks and server tools that make the job easier. 

In this article, we’re looking at the “enoent: no such file or directory” error when running Node.js web servers, its causes and what you can do to fix the problem.


What causes this error?

As the error message describes, the problem is caused by a missing configuration file or other dependencies required for your website or web app to run properly. 

Other possible causes include the following:

  • Incorrect file paths for web server configuration files. 
  • NPM configuration folders aren’t made.

Also read: Fix: An unexpected error is keeping you from copying the file


How to fix this?

Here are five fixes you can try out. 

Restart your PC

A quick and simple fix for the problem is to restart your PC. This ensures that any missing processes start up correctly alongside your operating system and that everything is in order before running your project.


Create an NPM directory

One of the main reasons you’d run into this error is that NPM doesn’t have a configuration directory to keep track of your project’s dependencies. 

The simplest way to fix this is to run the npm install command. If that doesn’t work, you can also try running npm update. Running both commands back to back can also sometimes fix the problem. 

Also read: Docs.google.com refused to connect: 6 Fixes


Check your configuration files

Take a good look at the directory that the error message shows. The file at the end is the missing dependency that’s causing this problem. While it can be any of the dependencies in your project, this is often a missing configuration file. 

To resolve the error, you can manually create the mentioned file path with the specified file at the end. Do keep in mind that while it might fix the problem we’re dealing with in this article, it can give you additional errors, as the resulting file will be empty. 

Make sure your configuration files are in the right place and using the right Node version.

You can then work your way around the new error, if any, by installing any required packages that might be missing from your project. 


Fix the file path

As an extension to the last solution, you can also try and manually fix the file path that the error has reported missing. The specific way to do this varies from framework to framework, so you’ll have to look up the documentation of your specific framework in use to check how to modify file paths.

If you’re sure that the configuration file or dependencies exist, you can manually point your web server to the right file. 


Reinitialise your project

You also might have to reinitialise your project to freshen up all the dependencies you’re using. This can be done by deleting the node_modules folder and the package-lock.json file. Just open a terminal, head over to your project’s root folder and run the following commands one at a time.

rm -rf node_modules package-lock.json
npm install

Error: Cannot find module express: 3 Fixes

If you get an error saying you don’t have appropriate permissions to carry out these tasks, prefix sudo to both commands. 

Also read: Fix: Unknown error: soap-error: encoding: object has no uirequestid property

Возможно, вам также будет интересно:

  • Ошибка engine workshop passat b5
  • Ошибка engine temperature too high
  • Ошибка engine power is reduced
  • Ошибка engine oil pressure fault stop
  • Ошибка engine oil level not when engine on

  • Понравилась статья? Поделить с друзьями:
    0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии