'How to resolve Nodejs: Error: ENOENT: no such file or directory

I have a Nodejs web app currently running on a server successfully. Now I'm trying to setup a local copy on my Development server.

I currently have Nodejs, NPM and Mongo Installed just like what I have in production server however the error below occurs when I try to start node server

Whats could be causing this issue?

embah@devsertwo:~/node/nodeapp$ node app.js
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
embah@devsertwo:~/node/nodeapp$


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]

[this is a helpful link on multer github][1]

but for me i have to create a public folder in the server folder also its like -cb(null,'public/'). [1]: https://github.com/expressjs/multer/issues/513

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:\Website\Nodemailer\Nodemailer-application\views\layouts\main.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.