Webpack dev服务器并做出反应。只在运行应用程序时获取目录列表

时间:2018-05-11 22:28:01

标签: javascript reactjs npm webpack webpack-dev-server

当我通过webpack-dev-server运行项目时,我在浏览器中看到的只是目录列表。我可以导航到文件并查看它们,但应用程序实际上并未运行。

当我只运行webpack时,输出文件格式正确,所以我不认为这是问题所在。我真的无法解决问题所在。

(webpack-dev-server和webpack命令通过添加到package.json中的脚本来运行。你可以在下面的package.json文件中看到它。)

What I get in the browser

我把项目分解成最简单的形式。它看起来像这样:

project file structure

app.js

console.log("Hello");

的package.json

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-scripts": "1.1.4",
    "webpack-cli": "^2.1.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "webpack": "webpack",
    "webpack-dev-server": "webpack-dev-server --hot -inline"
  },
  "devDependencies": {
    "babel-loader": "^7.1.4",
    "file-loader": "^1.1.11",
    "react-hot-loader": "^4.1.3",
    "webpack": "^4.8.2",
    "webpack-dev-server": "^3.1.4"
  }
}

webpack.config.js

module.exports = {
  context: __dirname + "/app",
  entry: "./app.js",
  output: {
    filename: "app.js",
    path: __dirname + "/dist",
  },
  module: {
  rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          loaders: ['babel-loader?presets[]=es2015,presets[]=react'],         
        },
      ],
    },
  mode: "development"
}

当我运行命令

时,这是我在终端的输出
npm run webpack-dev-server

> my-app@0.1.0 webpack-dev-server /home/chasmani/Documents/react/project
> webpack-dev-server --hot -inline

ℹ 「wds」: Project is running at http://localhost:8080/
ℹ 「wds」: webpack output is served from /
ℹ 「wdm」: Hash: 5a0c6d1388d655e14809
Version: webpack 4.8.2
Time: 1100ms
Built at: 2018-05-11 17:19:57
 Asset     Size      Chunks             Chunk Names
app.js  367 KiB  javascript  [emitted]  javascript
Entrypoint main = app.js
[../node_modules/ansi-html/index.js] 4.16 KiB {javascript} [built]
[../node_modules/loglevel/lib/loglevel.js] 7.68 KiB {javascript} [built]
[../node_modules/webpack/hot sync ^\.\/log$] ../node_modules/webpack/hot sync nonrecursive ^\.\/log$ 170 bytes {javascript} [built]
[0] multi ../node_modules/webpack-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./app.js 52 bytes {javascript} [built]
[../node_modules/sockjs-client/dist/sockjs.js] 176 KiB {javascript} [built]
[../node_modules/strip-ansi/index.js] 161 bytes {javascript} [built]
[../node_modules/url/url.js] 22.8 KiB {javascript} [built]
[../node_modules/webpack-dev-server/client/index.js?http://localhost:8080] ../node_modules/webpack-dev-server/client?http://localhost:8080 7.75 KiB {javascript} [built]
[../node_modules/webpack-dev-server/client/overlay.js] 3.58 KiB {javascript} [built]
[../node_modules/webpack-dev-server/client/socket.js] 1.05 KiB {javascript} [built]
[../node_modules/webpack/hot/dev-server.js] 1.66 KiB {javascript} [built]
[../node_modules/webpack/hot/emitter.js] 77 bytes {javascript} [built]
[../node_modules/webpack/hot/log-apply-result.js] 1.31 KiB {javascript} [built]
[../node_modules/webpack/hot/log.js] 1.03 KiB {javascript} [built]
[./app.js] 36 bytes {javascript} [built]
    + 13 hidden modules
ℹ 「wdm」: Compiled successfully.

我得到的只是文件系统。有人有什么想法吗?

2 个答案:

答案 0 :(得分:0)

感谢哈姆斯,他发表评论但未回答。

获取目录列表的原因是因为webpack-dev-server需要从index.html文件中调用js文件。

html-webpack-plugin是生成此index.html文件的一种方法。

答案 1 :(得分:0)

我遇到了上面显示的完全相同的问题,然后意识到我的问题是我在构建文件夹之外运行了服务命令“ serve -s”。那是错的。正确的方法是进入build文件夹,然后从那里运行“ serve -s”。