. Advertisement .
..3..
. Advertisement .
..4..
In its current version, JavaScript is an interpreted programming language developed from the concept of prototypes. This language is widely used for web pages as well as server-side. When users run the software, they may encounter the following error: “require() of ES modules is not supported. error with swagger-jsdoc“. It is a common mistake that all developers make. So, what’s causing this, and how can it be fixed? We’ll go over everything with you.
Why Does This Issue “require() of ES modules is not supported. error with swagger-jsdoc” Occur?
You are attempting to import swagger-jsdoc today, but you are getting an error.
You server.js file may like this:
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 the package.json file maybe like this:
{
"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 you try to run this project with “npm start”, you get this error:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:/Projects/swaggertest/node_modules/swagger-jsdoc/index.js require() of ES modules is not supported. require() of
You have encountered the error “require() of ES modules is not supported. error with swagger-jsdoc” because there is a problem with swagger-jsdoc.
Solutions For “require() of ES modules is not supported. error with swagger-jsdoc” Error
Best solution: Update swagger-jsdoc to 6.0.0.
And guess what? You might even be able to resolve it in a simple step. It is updating swagger-jsdoc to 6.0.0. This is only method for you to solve the error “require() of ES modules is not supported. error with swagger-jsdoc”.
A well-liked tool for creating documentation from comments in your app’s source code is called JSDoc. It serves two aims. First, anyone examining the source code has immediate access to the documentation. Second, a comprehensive set of reference materials can be created afterwards from the comments.
In your program, there appears to be a problem with swagger-jsdoc, so let’s downgrade to 6.0.0 to fix it. Here’s a look at your package. The json file now looks like this.
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.7",
"swagger-jsdoc": "6.0.0",
"swagger-ui-express": "^4.1.6"
}
This method is very simple, isn’t it? However, it will give you amazing result. Your error will be completely resolved after you do it. So, what are you waiting without applying it for your error to get your desired result?
Conclusion
JavaScript is one renowned programming language that plays an important role in “running” websites today. For those confused with this error, the method depicted in the figure above has proven to be the most effective for this error: “require() method of ES modules is not supported. a problem with swagger-jsdoc“. If you prefer different ideas or have frequent JavaScript questions, we have an active group of people who are always willing to help. Finally, we wish all readers a wonderful day full of creative code solutions.
Read more
Leave a comment