不支持ES模块的'require().',Node.js错误,表达,大张旗鼓的jsdoc [英] 'require() of ES modules is not supported. ' error with Node.js, express, swagger-jsdoc

查看:2404
本文介绍了不支持ES模块的'require().',Node.js错误,表达,大张旗鼓的jsdoc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试导入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:1108 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^

错误[ERR_REQUIRE_ESM]:必须使用导入来加载ES模块:/Users/me/Desktop/Projects/swaggertest/node_modules/swagger-jsdoc/index.js不支持ES模块的require().的require()/Users/me/Desktop/Projects/swaggertest/node_modules/swagger-jsdoc/index.js来自/Users/me/Desktop/Projects/swaggertest/app.js的ES模块文件,因为它是一个.js文件,其最近的父package.json包含类型":模块"将该包范围内的所有.js文件定义为ES模块.而是重命名index.js以.cjs结尾,更改要求代码使用import()或删除"type":"module"从/Users/me/Desktop/Projects/swaggertest/node_modules/swagger-jsdoc/package.json....代码:"ERR_REQUIRE_ESM"...

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?

推荐答案

我通过将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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆