this is not duplicate i have already did researched and checked other stack overflow links such as this, this and this
I imported swagger in existing Node.JS app like this
var swaggerJsdoc = require("swagger-jsdoc"),
var swaggerUi = require("swagger-ui-express");
but the error is on swagger-jsdoc the console is following
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. 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. 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.
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
npm install esm --save
//this also did not workBut 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
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
npm install swagger-jsdoc@6.0.1