简体   繁体   中英

How to resolve Node.js: "Error: 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

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 .

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 :)

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.

@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')

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')

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

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$

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.

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.

Running

npm run scss
npm run start 

in that order in terminal solved the issue for me.

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

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.

我在系统级别添加了 PM2_HOME 环境变量,现在它可以正常工作了。

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!

[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

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

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.

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.

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

It could not run the file since process.cwd() !== __dirname<\/code> .

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

我有一个节点版本不匹配,通过 nvm 安装正确版本的节点工作

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.

奇怪的是,在我的项目中,我总是在第一次添加/删除包时遇到此错误,但随后我再次运行相同的命令并且它在第二次运行时工作。

I had this error while uninstalling Bootstrap from the project so i just deleted the script and style from 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.

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

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';

If you have two terminals open, close one of it and type npm install. This will install the missing packages to your package.json. After that your project should compile and work without any problems.

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM