Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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?

like image 210
akasaa Avatar asked Mar 06 '21 17:03

akasaa


People also ask

How do you solve require () of ES modules is not supported?

You can solve the "[ERR_REQUIRE_ESM]: require() not supported" by doing one of two things: Use ESM - use import foo from 'foo' , instead of const foo = require('foo') and add the following line to your package. json file: "type": "module" . Downgrade to the last version of the package that is built with CommonJS .

How do I import node fetch with require?

Use ESM yourself. (Use import fetch from 'node-fetch' instead of const fetch = require('node-fetch') to import the package. You also need to put "type": "module" in your package. json and more. Follow the below guide.

Is require() of ES modules is not supported?

It'll be solved for you. the error Require () of ES modules is not supported occurs because you installed node-fetch version 3 and you can downgrade and install version 2. Your issue will be solved.

How to solve the [ERR_require_ESM] require() not supported error?

To solve the node-fetch error "[ERR_REQUIRE_ESM]: require () not supported", use a dynamic import to import the node-fetch package or downgrade the version of the package to 2.6.6, which is the last version that is built with CommonJS. Here is how to import the node-fetch package using a dynamic import in JavaScript and TypeScript.

Can I use CommonJS and es syntax in the same module?

The commonJS syntaxe, that uses require and module.exports, and the ES syntax, that use import * from "path" style of module. By default, nodejs will try to load modules with the CommonJS syntax. If you want to use the ES syntax, you must specify "type":"module" in your package.json. But you can't mix them up. You can use one syntax, but not both.

How to add ESM module to node server?

The esm module you’re adding is for adding ESM compatibility, but it’s unnecessary now that Node 12+ supports ESM natively; and it doesn’t work with ESM-only packages like node-fetch 3+. Remove the esm package. Add "type": "module" to your package.json. And that’s it. Then when you run node server.js it should work.


1 Answers

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"
  }
}
like image 155
akasaa Avatar answered Oct 20 '22 20:10

akasaa