简体   繁体   English

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

[英]Webpack dev server and react. Only get directory listing when running app

When I run the project through webpack-dev-server, all I see in the browser is the directory listing.当我通过 webpack-dev-server 运行项目时,我在浏览器中看到的只是目录列表。 I can navigate to the files and see them, but the app isn't actually running.我可以导航到文件并查看它们,但该应用程序实际上并未运行。

When I run just webpack, the output file is in the right format, so I don't think that is the issue.当我只运行 webpack 时,输出文件的格式正确,所以我认为这不是问题。 I really can't work out what the problem could be.我真的无法弄清楚问题可能是什么。

(The webpack-dev-server and webpack commands are run through adding to scripts in package.json. You can see it in the package.json file below). (webpack-dev-server 和 webpack 命令是通过添加到 package.json 中的脚本来运行的。你可以在下面的 package.json 文件中看到它)。

我在浏览器中得到的

I've broken the project down into it's simplest form.我已将项目分解为最简单的形式。 It looks like this:它看起来像这样:

项目文件结构

app.js应用程序.js

console.log("Hello");

package.json包.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 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"
}

And here is my output in terminal when I run the command这是我运行命令时在终端中的输出

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.

All I get is the file system.我得到的只是文件系统。 Anyone have any ideas?谁有想法?

Thanks to Hamms, who left a comment but not an answer.感谢哈姆斯,他发表了评论但没有回答。

The reason you get the directory listing is because webpack-dev-server needs the js files to be called from an index.html file.你得到目录列表的原因是 webpack-dev-server 需要从 index.html 文件调用 js 文件。

html-webpack-plugin is one approach to generating this index.html file. html-webpack-plugin是生成这个 index.html 文件的一种方法。

I faced the exact same problem shown above and then realized that my problem was that I was running the serve command "serve -s" from OUTSIDE the build folder.我遇到了上面显示的完全相同的问题,然后意识到我的问题是我正在从构建文件夹的外部运行服务命令“serve -s”。 That is wrong.那是错的。 The correct way is to go into the build folder and then run "serve -s" from there.正确的方法是进入构建文件夹,然后从那里运行“serve -s”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义Perf - 使用webpack-dev-server运行react app - Perf is not defined - running react app with webpack-dev-server 为什么使用Webpack-dev-server启动React App返回我的目录结构 - Why using Webpack-dev-server to start a React App returned my directory structure 获取在Webpack2中运行的Webpack-Dev-Server - Get Webpack-Dev-Server Running in Webpack2 Webpack2在运行webpack-dev-server时引发错误 - Webpack2 throws error when running webpack-dev-server 将react / node应用程序部署到heroku时出现webpack-dev-server错误 - webpack-dev-server error when deploying react/node app to heroku 使用Webpack在Django服务器上运行React App的问题 - Issue running React App on Django server with webpack React Webpack开发服务器可以工作,但是当我创建产品包时,出现参考错误“ require is not defined” - React webpack dev server works but when I create prod bundle I get reference error “require is not defined” WDS:已断开连接!同时运行webpack-dev-server时循环 - WDS: Disconnected! loop when running webpack-dev-server simultaneously 在Webpack包中运行Webpack Dev Server - Running a Webpack Dev Server inside a webpack bundle Electron, Webpack, 反应。 使用剪贴板时; 'fs' 未定义 - Electron, Webpack, React. When using clipboard; 'fs' is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM