首页 > 解决方案 > 运行swagger项目时出现节点js错误

问题描述

我正在为我的项目使用 swagger-ui-dist 文件夹。当我通过 npm run dev 运行它时,它运行良好,我可以看到所需的行为。

但是,当我将它提交到服务器时,有些东西会发生变化,并通过 npm run dev 在服务器上运行它会出现以下错误:


import path from "path";
       ^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Module._compile (/Users/myuserName/Desktop/APIs_Clone/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.newLoader [as .js] (/Users/myuserName/Desktop/APIs_Clone/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)

请帮助解决它,在此先感谢:)

下面也是我的 dev.babel.js 文件代码:

const path = require("path");
import { HotModuleReplacementPlugin } from "webpack"
import configBuilder from "./_config-builder"
import styleConfig from "./stylesheets.babel"

const devConfig = configBuilder(
  {
    minimize: false,
    mangle: false,
    sourcemaps: true,
    includeDependencies: true,
  },
  {
    mode: "development",
    entry: {
      "swagger-ui-bundle": [
        "./src/polyfills.js", // TODO: remove?
        "./src/core/index.js",
      ],
      "swagger-ui-standalone-preset": [
        "./src/polyfills", // TODO: remove?
        "./src/standalone/index.js",
      ],
      "swagger-ui": "./src/style/main.scss",
    },

    performance: {
      hints: false
    },

    output: {
      library: "[name]",
      filename: "[name].js",
      chunkFilename: "[id].js",
    },

    devServer: {
      port: 4493,
      publicPath: "/",
      disableHostCheck: true, // for development within VMs
      stats: {
        colors: true,
      },
      hot: true,
      contentBase: path.join(__dirname, "../", "dev-helpers"),
      host: "0.0.0.0",
    },

    plugins: [new HotModuleReplacementPlugin()],
  }
)

// mix in the style config's plugins and loader rules

devConfig.plugins = [...devConfig.plugins, ...styleConfig.plugins]

devConfig.module.rules = [
  ...devConfig.module.rules,
  ...styleConfig.module.rules,
]

export default devConfig

标签: javascriptnode.jsswaggernode-modulesswagger-ui

解决方案


Node 不支持开箱即用的 ES6 导入。此功能仍处于试验阶段

您有 2 个解决方案:

  • 使用require
const path = require("path");

编辑:
由于您的 babel 配置文件也使用 ES6 导入,因此您无法启动应用程序的构建过程。因此,为了在您的实际应用程序代码中使用 ES6 导入,您的构建脚本和配置只能使用require.

您应该将importdev.babel.js 文件中的语句替换为:

const { HotModuleReplacementPlugin } = require("webpack")
const configBuilder = require("./_config-builder")
const  styleConfig = require("./stylesheets.babel")

推荐阅读