简体   繁体   English

Swagger-JSdoc 不适用于 Node.JS 14.15.4 错误“ERR_REQUIRE_ESM”

[英]Swagger-JSdoc is not working on Node.JS 14.15.4 error 'ERR_REQUIRE_ESM'

this is not duplicate i have already did researched and checked other stack overflow links such as this , this and this这不是重复的,我已经研究并检查了其他堆栈溢出链接,例如thisthisthis

I imported swagger in existing Node.JS app like this我像这样在现有的 Node.JS 应用程序中导入了 swagger

var swaggerJsdoc = require("swagger-jsdoc"),
var swaggerUi = require("swagger-ui-express");

but the error is on swagger-jsdoc the console is following但错误出现在控制台正在跟踪的 swagger-jsdoc 上

internal/modules/cjs/loader.js:1080 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); internal/modules/cjs/loader.js:1080 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^ ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\myTestProject\node_modules\swagger-jsdoc\index.js require() of ES modules is not supported.错误 [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\myTestProject\node_modules\swagger-jsdoc\index.js 不支持 ES 模块的 require()。 require() of D:\myTestProject\node_modules\swagger-jsdoc\index.js from D:\myTestProject\index.js is an ES module file as it is a.js file whose nearest parent package.json contains "type": "module" which defines all.js files in that package scope as ES modules.来自 D:\myTestProject\index.js 的 D:\myTestProject\node_modules\swagger-jsdoc\index.js 的 require() 是一个 ES 模块文件,因为它是一个 .js 文件,其最近的父 package.json 包含“类型”: “模块”将 package scope 中的 all.js 文件定义为 ES 模块。 Instead rename D:\myTestProject\node_modules\swagger-jsdoc\index.js to end in.cjs, change the requiring code to use import(), or remove "type": "module" from D:\myTestProject\node_modules\swagger-jsdoc\package.json.而是将 D:\myTestProject\node_modules\swagger-jsdoc\index.js 重命名为以.cjs 结尾,将需要的代码更改为使用 import(),或从 D:\myTestProject\node_modules\swagger 中删除 "type": "module" -jsdoc\package.json。

 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Object.<anonymous> (D:\myTestProject\index.js:3:16) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) { code: 'ERR_REQUIRE_ESM' }

what I already did is tried the following steps but they did not work我已经尝试了以下步骤,但它们没有用

  1. Added flag '--experimental-modules' in node starting script在节点启动脚本中添加了标志“--experimental-modules”
  2. npm install esm --save //this also did not work npm install esm --save //这也不起作用

But now finally i found a solution which not requires you to rename your.js files to.mjs also it will not require your node to be upgraded if you are alraedy running Node.Js version 12 or above但是现在我终于找到了一个解决方案,它不需要您将您的.js 文件重命名为.mjs,如果您已经运行 Node.Js 版本 12 或更高版本,它也不需要升级您的节点

simply downgrade your swagger-jsdoc to 6.1.0, which is built with CommonJS tools, at first i was using swagger-jsdoc 7.x which was not built using CommonJS tools to downgrade the swagger-jsdoc use the following command of npm只需将您的 swagger-jsdoc 降级到使用CommonJS工具构建的 6.1.0,起初我使用的是不是使用CommonJS工具构建的 swagger-jsdoc 7.x 来降级 swagger-jsdoc 使用 npm 的以下命令

npm install swagger-jsdoc@6.0.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 '不支持 ES 模块的 require()。 ' Node.js、快递、swagger-jsdoc 出错 - 'require() of ES modules is not supported. ' error with Node.js, express, swagger-jsdoc require('node-fetch') 给出 ERR_REQUIRE_ESM - require('node-fetch') gives ERR_REQUIRE_ESM 错误 [ERR_REQUIRE_ESM]:ES 模块的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module 错误 [ERR_REQUIRE_ESM]:如何在节点 12 中使用 es6 模块? - Error [ERR_REQUIRE_ESM]: How to use es6 modules in node 12? 在 Next.js ERR_REQUIRE_ESM 中导入 ES 模块 - Import ES module in Next.js ERR_REQUIRE_ESM Next.js 上的 ERR_REQUIRE_ESM 和 Package.json 错误。 更新后。 怎么修? - ERR_REQUIRE_ESM and Package.json error on Next.js. after updating. How to fix? 错误 [ERR_REQUIRE_ESM]:不支持来自 /app/commands/Image/meme.js 的 ES 模块 /app/node_modules/got/dist/source/index.js 的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/got/dist/source/index.js from /app/commands/Image/meme.js not supported 错误 [ERR_REQUIRE_ESM] Github 部署错误 - Error [ERR_REQUIRE_ESM] Github deploy error PM2 带 ES 模块。 错误:ERR_REQUIRE_ESM - PM2 with ES module. Error: ERR_REQUIRE_ESM 升级 Node JS 和 ERR_REQUIRE_ESM:必须使用 import 加载 ES Module: 'inheritsloose.js' of Babel Runtime - Upgrading Node JS and ERR_REQUIRE_ESM: Must use import to load ES Module: 'inheritsloose.js' of Babel Runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM