问题描述
我尝试导入swagger-jsdoc,但出现错误.我在互联网上搜索,但其他解决方案对我不起作用.
I trying to import swagger-jsdoc but I get an error. I searched on internet but other solutions not work for me.
我的server.js文件如下:
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');
})
还有我的package.json文件:
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"
}
}
但是,当我尝试使用"npm start"运行该项目时,我收到此错误:
But when I try to run this project with "npm start" I getting this error:
node:内部/模块/cjs/loader:1108抛出新的ERR_REQUIRE_ESM(文件名,parentPath,packageJsonPath);^
node:internal/modules/cjs/loader:1108throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);^
我该如何解决这个问题?
How can I solve this issue?
推荐答案
我通过将swagger-jsdoc降级到6.0.0解决了这个问题
I solved this issue with downgrade swagger-jsdoc to 6.0.0
所以,我的package.json文件现在看起来像:
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"
}
}
这篇关于不支持ES模块的'require().',Node.js错误,表达,大张旗鼓的jsdoc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!