简体   繁体   中英

'require() of ES modules is not supported. ' error with Node.js, express, swagger-jsdoc

I trying to import swagger-jsdoc but I get an error. I searched on internet but other solutions not work for me.

My server.js file like that:

const express = require('express');
const app = express();
const swaggerJsDoc = require('swagger-jsdoc');

const port = 3000;

app.get('/customers', (req,res) => {
    res.send('Customers Route');
})

app.listen(port, ()=> {
    console.log('Server listening on 3000');
})

And my package.json file like that:

{
  "name": "swaggertest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.7",
    "swagger-jsdoc": "^7.0.0-rc.4",
    "swagger-ui-express": "^4.1.6"
  }
}

But when I try to run this project with "npm start" I getting this error:

node:internal/modules/cjs/loader:1108 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/me/Desktop/Projects/swaggertest/node_modules/swagger-jsdoc/index.js require() of ES modules is not supported. require() of /Users/me/Desktop/Projects/swaggertest/node_modules/swagger-jsdoc/index.js from /Users/me/Desktop/Projects/swaggertest/app.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 index.js to end in.cjs, change the requiring code to use import(), or remove "type": "module" from /Users/me/Desktop/Projects/swaggertest/node_modules/swagger-jsdoc/package.json. ... code: 'ERR_REQUIRE_ESM'...

How can I solve this issue?

I solved this issue with downgrade swagger-jsdoc to 6.0.0

So, my package.json file now look like:

{
  "name": "swaggertest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.7",
    "swagger-jsdoc": "6.0.0",
    "swagger-ui-express": "^4.1.6"
  }
}

This means your library no more supports require and you need to use ES 6 syntax

import XXX from "node-module";

Going to previous versions may help but only temporarily.

To be able to use import you also need to change package.json

{ 
   "type": "module",
 }

There is a bit of learning here which may help in the longer run. https://nodejs.org/api/packages.html#packages_type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM