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

node.jsNpmEnoent

node.js Problem Overview


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$

node.js Solutions


Solution 1 - node.js

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 - node.js

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 - node.js

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 - node.js

@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 - node.js

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 - node.js

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 - node.js

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 - node.js

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 - node.js

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 - node.js

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 - node.js

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 - node.js

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

Solution 13 - node.js

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 - node.js

Running

npm run scss
npm run start 

in that order in terminal solved the issue for me.

Solution 15 - node.js

[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 - node.js

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 - node.js

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 - node.js

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

Solution 19 - node.js

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 - node.js

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 - node.js

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 - node.js

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 - node.js

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmeka MbahView Question on Stackoverflow
Solution 1 - node.jsTrottView Answer on Stackoverflow
Solution 2 - node.jsollehView Answer on Stackoverflow
Solution 3 - node.jsOdiljon DjamalovView Answer on Stackoverflow
Solution 4 - node.jsAniruddha PandeyView Answer on Stackoverflow
Solution 5 - node.jsChase DeneckeView Answer on Stackoverflow
Solution 6 - node.jsDamian GreenView Answer on Stackoverflow
Solution 7 - node.jsMD.FAHIM HOSSENView Answer on Stackoverflow
Solution 8 - node.jsHasnaa IbraheemView Answer on Stackoverflow
Solution 9 - node.jsAnalystView Answer on Stackoverflow
Solution 10 - node.jsJavadView Answer on Stackoverflow
Solution 11 - node.jssanghamitra Ravi KantView Answer on Stackoverflow
Solution 12 - node.jskepy97View Answer on Stackoverflow
Solution 13 - node.jsRatnesh RaiView Answer on Stackoverflow
Solution 14 - node.jsOnat KorucuView Answer on Stackoverflow
Solution 15 - node.jsBUGATTI AJAYView Answer on Stackoverflow
Solution 16 - node.jsChukwunazaekpereView Answer on Stackoverflow
Solution 17 - node.jsNivView Answer on Stackoverflow
Solution 18 - node.jsLukeWarm2897View Answer on Stackoverflow
Solution 19 - node.jsTimJ0212View Answer on Stackoverflow
Solution 20 - node.jsMichal KurzView Answer on Stackoverflow
Solution 21 - node.jsYasir AkbarView Answer on Stackoverflow
Solution 22 - node.jsmsalihbindakView Answer on Stackoverflow
Solution 23 - node.jsBlank456View Answer on Stackoverflow