javascript - 运行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
解决方案
Node 不支持开箱即用的 ES6 导入。此功能仍处于试验阶段
您有 2 个解决方案:
- 使用
require
:
const path = require("path");
- 您可以尝试使用 babel 设置您的项目,这会将您的代码转换为 NodeJS 支持的内容:https ://babeljs.io/en/setup
编辑:
由于您的 babel 配置文件也使用 ES6 导入,因此您无法启动应用程序的构建过程。因此,为了在您的实际应用程序代码中使用 ES6 导入,您的构建脚本和配置只能使用require
.
您应该将import
dev.babel.js 文件中的语句替换为:
const { HotModuleReplacementPlugin } = require("webpack")
const configBuilder = require("./_config-builder")
const styleConfig = require("./stylesheets.babel")
推荐阅读
- android - 如何从 Firebase 数据库中删除此特定节点?
- javascript - Jmeter 脚本重放问题 - 请启用 JavaScript 以查看页面内容
- swift - 使用带有布尔运算符的字符串 Swift
- c++ - 裁剪并显示我从 Haar Cascade 获得的图像
- c - C 堆栈变量的返回地址 = NULL?
- c# - 将项目添加到列表后使用数据绑定更新列表视图
- java - Java List 中的 C# IEnumerable 方法等效项
- javascript - 包裹在大括号内的反应组件道具?
- php - 如何在大型数据集的 codeigniter 中使用 dompdf 分块和生成 pdf
- google-search-console - 提交两者 www。和非 www 版本的网站到 Google Search Console