Internal/module/cjs/loader.js: 582 throw err

我正在跟踪控制台错误。 Error : Cannot find module

这是我在控制台中得到的完整错误。我应该怎么做?

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)
507590 次浏览

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

For All = > Windows,Linux,Mac

  1. 删除 node_modules目录
  2. 删除 package-lock.json文件
  3. 运行 npm install
  4. 运行 npm start

为了 Linux

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

帮助我的是将我正在处理的 .js文件放到一个新的文件夹中,将该文件夹拖放到 VS Code 中(直接在 VS Code 中打开目录) ,在 VS Code 中打开终端,然后简单地键入 node <filename>.js(或者在我的例子中键入 node index.js)。

我已经在我的系统上安装了 node,但是不管出于什么原因,我仍然得到了您提到的错误,即使当我键入到文件的直接路径,即 node /desktop/index.js

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

package.json({"npm": <your server code file name>.js})中的文件名替换为正在运行服务器代码的文件(应该是 app.jsmain.jsstart.jsserver.js,或者任何您选择的文件)。

特别的。Js 文件位于应用程序的子文件夹(/src)中,而 Terminal 位于一般的 App 文件夹中。(包含所有包文件、模块、公用文件夹、 src 文件夹)它抛出了这个错误。转到(/src)应用程序解决了我的问题。

我也有同样的错误,因为我的文件名后面有一个空格(不是引用,而是实际的文件名)。一旦我把 app.js 改成 app.js,它就能正常工作了。

同样的事情发生在我身上,我只是解决了删除“ dist”文件,并重新运行应用程序。

对我来说,我试图使用的 Node 包只能在 Node 的旧版本上工作。

我能够通过使用 自酿的安装旧版本的 Node 来修复它:

brew unlink node
brew install node@12
echo 'export PATH="/usr/local/opt/node@12/bin:$PATH"' >> ~/.zshrc

在上面的命令中,您需要编辑 Node 版本,然后导出 PATH 命令。

下面的命令为我解决了这个问题。

npm install node-gyp -g
npm install bcrypt -g


npm install bcrypt -save

我卸载 puppeteer, mocha and chai使用

npm uninstall puppeteer mocha chai

然后重新安装 使用

npm install puppeteer mocha chai

而错误消息根本就没有显示出来

昨晚发生了一些奇怪的事。 我运行命令 node run watch而不是 npm run watch。 我试过在这个帖子上做所有的事情,但是一无所获。我很沮丧,但最终注意到我运行错误的命令。我笑得很大声。有时候会发生这种事。不过很享受学习 Nodejs。

尝试下面的命令

remove node_modules and package-lock.json

rm -rf node_modules package-lock.json

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

npm install

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

npm start

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

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

我得到了同样的错误:

 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:\Programming\01a.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...

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

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

回到这里:

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

在我做了这个更改并使用 npm run dev重新启动服务器之后,一切工作正常。

我改变了我的项目文件夹的名称,它的工作,我不知道为什么:)

当我使用下面的命令时,我也得到了同样的错误:

node .function-hello.js

I changed my command to below command, it worked fine:

node .\function-hello.js

对于我来说“ npm 安装”从命令提示符再次工作。命令提示符必须是“作为管理员运行”

您需要启动服务器使用跟随命令

npm start

或者

yarn start

The path to the js file you're trying to execute is wrong; you have to type the path and the file name you want to execute relative to root where node is, but what you typed isn't where it is.

我输入了 node redux-basics.js,得到了这个稍微有点误导性的错误消息,Stack Overflow‘ ed,查看了我的文件系统,我应该输入 node src/redux-basics.js

在主目录中创建. js 文件,而不是在诸如 public 或 src 之类的子文件夹中创建。

此错误消息很容易重现。

  • Open a terminal window.
    (Windows: WinKeycmdEnter. 在 Linux 上: Ctrl + Alt + t。)
  • 键入 npm并按下 Enter查看是否安装了 Node.js。
  • 如果你得到了 command not found,下载地址是 < a href = “ https://nodejs.org/en/download/”rel = “ nofollow norefrer”> https://nodejs.org/en/download/ 安装。
    (在 Linux/Ubuntu 上: 如果您愿意,可以使用 sudo apt install nodejs。)
  • 键入(或粘贴) node thisFileDoesNotExist.js(并按 Enter)。

在 Windows 上,希望看到类似于下面这样的东西:

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


Error: Cannot find module [... + a few more lines]

在 Linux 上(Ubuntu 18.04) :

module.js:549
throw err;
^


Error: Cannot find module [...]

我还没有尝试过 macOS,但是希望在那里也能看到类似的东西。

注意 : 在调试时,这可能没有明显的原因 在 VisualStudio 代码中。
如果在 VScode 中得到错误,请查看答案是否由 快乐手31 有用吗。


最后,要在终端 没有中运行 Node.js, 在 Windows 终端(命令行)中尝试:

echo console.log('\nHello world!')> hello.js
node hello.js

In the Linux terminal try:

echo "console.log('\nHello world\!\n')"> hello.js
node hello.js

当然,希望看到终端机响应:

Hello world!

Caseyustus 的评论帮助了我。 显然我的需求路径上有空间。

const listingController = require("../controllers/ listingController");

我把密码改成了

const listingController = require("../controllers/listingController");

一切都很好。

在我做了 sudo npm i cjs-loader (并确保安装 Express,而不仅仅是 Express-http-agent)之后,它终于对我起作用了

对于那些正在使用 TypeScript的人来说,这是由你设置的 compilerOptions中的 incremental选项引起的。

这将导致构建存储缓存所有数据的 tsconfig.tsbuildinfo文件。如果您删除该文件并重新编译该项目,它应该立即工作。

遇到了类似的问题,没有恶魔跑过码头,

值得检查一下 package.json 中的“ main”文件是否配置为指向正确的入口点

在包裹里 Json

"main": "server.js",
"scripts": {
"start":"nodemon src/server.js",
"docker:build": "docker build -f ./docker/Dockerfile . "
},

只要输入“ Node NodeJsProject/app.js

您的程序将运行:)

您可能没有从正确的目录运行终端命令。

例如,如果您已经创建了一个新文件夹,请考虑导航到该文件夹,然后从该文件夹运行命令。

转到 package.json 文件并检查下面一行:

"main": "main.js

括号中提供的文件必须正是要运行的文件

node main.js

这解决了我的问题

I've got this same issue when working in typescript with nestjs. 不管出于什么原因,在第一次尝试在 VS Code 中运行调试之后(使用 launch.json-顺便说一句,它不能工作)

对我起作用的是: remove dist folder (the one with generated .js files from .ts files, path to this folder is usually specified in tsconfig.json).

简单的解决方案,但是很有效:)

如果使用 node/Express,请考虑在终端中运行此命令

npm i cjs-loader

我不小心将一个文件移到/src 之外,这改变了结果/build 目录的结构,所以现在在 package.json 中... ... "dev:serve": "nodemon --inspect=4899 build/index.js",不存在了... ... 它现在在 build/src/index.js 中

For me I just installed the package and it worked:

yarn add cjs-loader --save

或者

npm i cjs-loader --save

But notice that sometimes the problem is not only in the absence of this lib, but the path of your application file app.js may not be inside this folder:

C:\Users\User\Desktop\NodeJsProject\

在修复了 add/delete 程序中的 node.js 安装之后,它就运行了。

这是全球性的,不只是一个项目!

对我来说,我的问题是与我的 Procfile。我有

web: node src/index.js

当我应该:

web: node dist/index.js

在修补节点14并尝试让 CJS 和 MJS 同时工作时,我设法在 server目录中添加了 package.json 文件

{ type: "module" }

我忘了我添加了这个文件,这导致我在查看这个错误上浪费了几个小时。也许这能帮到别人!

我将 server.js (无论您的情况如何)文件放在了错误的 XD 目录中

我用 ts-node而不是 tsc && node解决了这个问题

First delete/remove node_modules file, then run this code:

Npm 缓存清除力

安装力

NPM 开始

我希望这能解决你的问题

我的错误是: 已连接调试器。 等待调试器断开连接..。 Node: Internal/module/cjs/loader: 936 投掷错误; ^ Error: Cannot find module 'C:\Users\joel.sebastian\AppData\Roaming\npm\node_modules\azure-functions-core-tools\lib\main.js'........{ 代码: ‘ MODULE _ NOT _ FOUND’, requireStack: [] }

出问题的是天蓝色功能工具的安装,我通过以下方式安装: Npm install-g 蔚蓝色-功能-核心-工具@3——不安全-烫发 如果再次显示文件错误,添加强制: Npm install-g 蔚蓝色-功能-核心-工具@3——不安全-烫发真正——力量

通常对我来说,这是因为天蓝色的功能工具模块

Debugger attached. 等待调试器断开连接..。 Internal/module/cjs/loader.js: 905 投掷错误;

Error: Cannot find module 'C:\Users\myName\AppData\Roaming\npm\node_modules\azure-functions-core-tools\lib\main.js'

因此,您可以找到最后一行中提到的缺少的模块,并安装或更新它

对我来说,把我的工作目录移动到 node 所在的位置就解决了问题。

我的节点位置 -C:\Users\hp

我过去的工作目录位置 abc 0

我的新工作目录路径 abc 0

然后从新位置打开目录,再次删除现在的 node modulespackage-lock.json和 npm install。

现在运行 node <filename>.js,砰!