如何解决Nodejs:错误:ENOENT:没有这样的文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43260643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:09:23  来源:igfitidea点击:

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

node.jsmongodbnpm

提问by Emeka Mbah

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.

我有一个 Nodejs Web 应用程序当前在服务器上成功运行。现在我试图在我的开发服务器上设置一个本地副本。

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

我目前安装了 Nodejs、NPM 和 Mongo,就像我在生产服务器中安装的一样,但是当我尝试启动节点服务器时会发生以下错误

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$

回答by Trott

Your app is expecting to find a file at /home/embah/node/nodeapp/config/config.jsonbut that file does not exist (which is what ENOENTmeans). 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.

您的应用程序希望找到一个文件,/home/embah/node/nodeapp/config/config.json但该文件不存在(这就是什么ENOENT意思)。因此,您要么需要创建预期的目录结构,要么配置您的应用程序,使其在正确的目录中查找config.json.

回答by olleh

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

我不知道,但它可能会帮助某人:)

回答by Odiljon Djamalov

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.

92% 额外的资产处理脚本-webpack-plugin × ?wdm?: Error: ENOENT: no such file or directory, open....==> 如果有人遇到这样的错误,你应该做以下事情:1) 你应该检查如果文件路径在 angular.json 文件中正确。

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

2) 你应该按 crtl+c 并重新运行项目。

回答by Damian Green

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.

我也遇到了这个问题,因为我打开了另一个运行应用程序的控制台窗口,我试图在另一个控制台窗口中重新运行 yarn start。

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

第一个纱线执行阻止了第二个纱线的写入。所以我只是杀死了第一个进程并且它起作用了

回答by Aniruddha Pandey

@ollehanswer worked because npm installwill create a node_modulesdirectory in the current path where it is executed. So, while using the file server system module, when you declare

@olleh答案有效,因为npm install它将node_modules在执行它的当前路径中创建一个目录。因此,在使用文件服务器系统模块时,当您声明

const fs = require('fs')

this locate files from the top level directory of node_modules folder.

这从 node_modules 文件夹的顶级目录中找到文件。

回答by Hasnaa Ibraheem

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

就我而言,问题发生在我从 git 分支切换到另一个分支后,对一些旧文件的引用保留在“node_modules/.cache”目录中的脚本中。
删除“node_modules”、“build”目录和“package-lock.json”文件然后发出“npm install”命令为我解决了这个问题

回答by Blank456

I tried something and got this error Error: ENOENT: no such file or directory, open 'D:\Website\Nodemailer\Nodemailer-application\views\layouts\main.handlebars'

我尝试了一些东西并得到这个错误错误:ENOENT:没有这样的文件或目录,打开'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.

我得到的解决方法是按字面意思构建目录。这意味着完全按照所示标记项目,奇怪的是我给了计算机想要的东西。

回答by Yasir Akbar

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

它发生在我身上。我错误地删除了一些css文件然后复制回来。这个错误当时就出现了。所以我重新启动了我所有的 docker 和其他服务器,然后它就消失了,也许这对某人有所帮助:)

回答by msalihbindak

In my case

就我而言

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

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

gave me the error.

给了我错误。

I solved it with change the address like this:

我通过更改地址解决了这个问题:

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

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

回答by Leke

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.

如果您打开了两个终端,请关闭其中一个并键入 npm install。这会将丢失的软件包安装到您的 package.json 中。之后,您的项目应该可以编译并正常工作。