0

我无法将 swagger-jsdoc 导入或要求到我的 node express web 应用程序。

系统信息操作系统:

- MacOS Big Sur

套餐:

- "swagger-jsdoc": "^7.0.0-rc.6"
- "webpack": "^5.23.0"
- "node": "v12.19.0" ​

在我的 Web 应用程序中,我想使用 swaggerUI 为我的 API 创建文档。但是我的代码的导入语句从 json-loader 引发了以下错误。


import swaggerJsdoc from 'swagger-jsdoc';


引发以下错误:

Error: Module parse failed: Unexpected token m in JSON at position 0 while parsing near 'module.exports = {"i...'
File was processed with these loaders:
 * ./node_modules/json-loader/index.js


const swaggerJsdoc = require("swagger-jsdoc");


引发以下错误

internal/modules/cjs/loader.js:1031
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /path/to/project/node_modules/swagger-jsdoc/index.js
require() of ES modules is not supported.
require() of /path/to/project/node_modules/swagger-jsdoc/index.js from /path/to/project/dist/index.bundle.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 /path/to/project/node_modules/swagger-jsdoc/package.json.

我在谷歌上做了一些挖掘,找到swagger-jsdoc-webpack-plugin了 webpack 插件并安装了这个插件,然后在我的 webpack.config.json 上添加了以下配置

const SwaggerJSDocWebpackPlugin = require('swagger-jsdoc-webpack-plugin');

module.exports = {
  ...
  plugins: [
    new SwaggerJSDocWebpackPlugin({
      swaggerDefinition: {
        openapi: '3.0.0',
        info: {
          title: 'Title',
          version: '1.0.0',
          description: 'Description',
        },
      },
      apis: ['./src/routes/**/*.js, ./src/modules/**/*.js', './src/index.js'],
    }),
  ],

虽然,它在我的情况下也不起作用。

我是 nodeJS、JS 和 webpack 的新手。因此,我有点卡在哪里寻找我的问题的解决方案。任何想法,将不胜感激。

谢谢

4

0 回答 0