运行swagger项目时节点js错误

时间:2019-10-03 08:33:46

标签: javascript node.js swagger node-modules swagger-ui

我正在为我的项目使用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

1 个答案:

答案 0 :(得分:0)

Node不支持开箱即用的ES6导入。 This feature is still experimental

您有2个解决方案:

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

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

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

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