Javascript 内部/模块/cjs/loader.js:582 抛出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53545800/
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-08-23 05:08:01  来源:igfitidea点击:

internal/modules/cjs/loader.js:582 throw err

javascriptnode.jsexpressroutingbackend

提问by istiyak ahamed Milon

I'm getting following Console Error. Error : Cannot find module

我正在关注控制台错误。 Error : Cannot find module

Here is the full error i'm getting in console. What should I do?

这是我在控制台中遇到的完整错误。我该怎么办?

internal/modules/cjs/loader.js:582
    throw err;
    ^

Error: Cannot find module 'C:\Users\User\Desktop\NodeJsProject\app.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)
    at Function.Module._load (internal/modules/cjs/loader.js:506:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

采纳答案by Hashan Gunathilaka

I had the same issue when I first tried on node js.
I noticed this issue was happening to me because I had some .js files with same names in different directories, which were in the same main directory.
I created another directory outside the main project folder, and created a .js file.
After that, it ran fine.
ex- app.js

我第一次尝试 node js 时遇到了同样的问题。
我注意到这个问题发生在我身上,因为我在不同的目录中有一些同名的 .js 文件,它们在同一个主目录中。
我在主项目文件夹之外创建了另一个目录,并创建了一个 .js 文件。
之后,它运行良好。
前 app.js

回答by Parth kharecha

  1. Delete the node_modulesdirectory
  2. Delete the package-lock.jsonfile
  3. Run npm install
  4. Run npm start
  1. 删除node_modules目录
  2. 删除package-lock.json文件
  3. npm install
  4. npm start

OR

或者

rm -rf node_modules package-lock.json && npm install && npm start

回答by Mac Rathod

try following command

尝试以下命令

remove node_modulesand package-lock.json

删除node_modulespackage-lock.json

rm -rf node_modules package-lock.json

then run following command to install dependencies

然后运行以下命令安装依赖项

npm install

finally, run your package by following command.

最后,通过以下命令运行你的包。

npm start

回答by HappyHands31

What helped me was to place the .jsfile that I was working with in a new folder, drag and drop that folder into VS Code (to open the directory directly in VS Code), open the terminal in VS Code, and then simply type node <filename>.js(or in my case node index.js).

帮助我的是将.js我正在使用的文件放在一个新文件夹中,将该文件夹拖放到 VS Code 中(直接在 VS Code 中打开目录),在 VS Code 中打开终端,然后只需键入node <filename>.js(或在我的情况下node index.js)。

I had already installed nodeon my system, but for whatever reason, I was still getting the error that you've mentioned, even when I typed the direct path to the file i.e. node /desktop/index.js.

我已经安装node在我的系统上,但无论出于何种原因,我仍然收到您提到的错误,即使我输入了文件的直接路径,即node /desktop/index.js.

So, creating a new folder on my desktop, placing the .jsfile inside that folder, opening that folder within VS Code, and then typing node index.jsin the terminal solved my issue.

因此,在我的桌面上创建一个新文件夹,将.js文件放在该文件夹中,在 VS Code 中打开该文件夹,然后node index.js在终端中输入解决了我的问题。

回答by shr_kuldeep

Replace your file name in package.json({"npm": <your server code file name>.js}) with that file where your server code is running (it should be app.js, main.js, start.js, server.js, or whatever you picked up).

在替换您的文件名package.json{"npm": <your server code file name>.js}在您的服务器代码运行与该文件)(它应该是app.jsmain.jsstart.jsserver.js,或你拿起)。

回答by caseyjustus

I was having the same error because I had a space at the end of my filename(not the reference but the actual filename). Once I changed 'app.js ' to 'app.js' it worked fine.

我遇到了同样的错误,因为我的文件名末尾有一个空格(不是引用而是实际文件名)。一旦我将“app.js”更改为“app.js”,它就可以正常工作。

回答by Murali Uppangala

Hashan Gunathilaka was correct. this strange behavior exists. I just deleted the duplicates and closed the command prompt to open again in windows. Worked fine.

哈山·古纳提拉卡是对的。这种奇怪的行为存在。我刚刚删除了重复项并关闭了命令提示符以在 Windows 中再次打开。工作得很好。

回答by ttfreeman

Make sure you give the right address path for app.jswhen running node <path>/app.js. It can't find it

确保app.js在运行时提供正确的地址路径node <path>/app.js。它找不到它

Error: Cannot find module 'C:\Users\User\Desktop\NodeJsProject\app.js'

回答by dragon

I got the same error:

我得到了同样的错误:

 nodemon -w server.js server.js

[nodemon] 2.0.2
[nodemon] reading config .\nodemon.json
[nodemon] to restart at any time, enter `rs`
[nodemon] or send SIGHUP to 19248 to restart
[nodemon] ignoring: .\node_modules/**/* .\.next/**/*
[nodemon] watching dir(s): server.js
[nodemon] watching extensions: js,json
[nodemon] starting `node server.js index.js`
[nodemon] forking
[nodemon] child pid: 18840
[nodemon] watching 30 files
internal/modules/cjs/loader.js:797
    throw err;
    ^

Error: Cannot find module 'D:\Programminga.nextjs\project\index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
    at Function.Module._load (internal/modules/cjs/loader.js:687:27)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
[nodemon] app crashed - waiting for file changes before starting...

I followed all the advises from here, but none of them worked for me. What I found is that I moved the server.js in his own folder server/server.js, but in package.json I forgot to make the change from this:

我遵循了这里的所有建议,但没有一个对我有用。我发现我将 server.js 移动到了他自​​己的文件夹 server/server.js 中,但是在 package.json 中我忘记从这里进行更改:

 "dev": "nodemon -w server.js server.js",
 "build": "next build",
 "start": "NODE_ENV=production node server.js"

to this:

对此:

"dev": "nodemon -w server/server.js server/server.js",
"build": "next build",
"start": "NODE_ENV=production node server/server.js"

After I made this change and restart the server with npm run deveverything worked fine.

在我进行此更改并重新启动服务器后,npm run dev一切正常。

回答by Mahdi Safarmohammadloo

I changed name of my Project's folder and It's worked , i don't know why :)

我更改了我的项目文件夹的名称并且它起作用了,我不知道为什么:)