javascript - Webpack - net::ERR_ABORTED 404(未找到)
问题描述
我正在学习Webpack,在运行Webpack并npm run build
创建bundle.js文件,然后导入Post模块,然后导出到Index.js文件,然后将bundle.js文件包含到index.html文件中,这是我的代码
Post.js
export default class Post {
constructor(title) {
this.title = title
this.date = new Date()
}
toString() {
return JSON.stringify({
title: this.title,
date: this.date.toJSON()
})
}
}
index.js
import Post from "./Post"
const post = new Post('Webpack Post Title')
console.log('Post to String', post.toString())
索引.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="analytics.js"></script>
</head>
<body>
<div class="container">
<h1>Webpack Course</h1>
</div>
<script src="bundle.js"></script>
</body>
</html>
之后我用实时服务器打开文件,它给出了一个错误
GET http://127.0.0.1:5500/src/bundle.js net::ERR_ABORTED 404(未找到)
未经检查的 runtime.lastError:消息端口在收到响应之前关闭。
webpack.config.js
const path = require('path');
module.exports = {
mode: "development",
entry: "./src/index.js",
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
}
包.json
{
"name": "tutorial-minin",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Suro Zakaryan",
"license": "ISC",
"devDependencies": {
"webpack": "^5.11.1",
"webpack-cli": "^4.3.1"
}
}
解决方案
添加<base href="/">
到您的 index.html头应该可以解决问题。
推荐阅读
- azure - 新 Microsoft.Azure.Cosmos SDK 中的 CompositeToken
- ruby-on-rails - 如何在用户登录前允许查看指定资源?
- postgresql - 使用 PostgresSQL 动态转置表
- java - 如何创建我自己的 PageSize 变量以使用 iTextpdf 创建 PDF 文档
- node.js - 使用节点核心 http 并查看响应。响应json在哪里?
- python - 将表从一个数据库复制到另一个
- python - Pyaudio:使用 WASAPI 驱动程序时,音频播放听起来很糟糕
- flutter - Equatable 的子类将什么传递给超级(Equatable 类)?
- c# - System.Text.Json.JsonElement ToObject 解决方法
- codenameone - 为什么 JSONParsing 在 Codenameone 中失败