简体   繁体   English

Webpack 开发服务器编译成功但重新加载或重建不起作用

[英]Webpack dev server compiling successfully but reload or rebuild not working

I'm having problems with the webpack dev server, when running the script npm run frontend (during development) to see changes in the browser, it shows it successfully compiles.我在使用 webpack 开发服务器时遇到问题,当运行脚本 npm 运行前端(在开发期间)以查看浏览器中的更改时,它显示它已成功编译。 terminal compiled successfully终端编译成功

However, the automatic build and browser reload don't work, the only way I can see the changes I made in the browser is by running npm run build every time I save.但是,自动构建和浏览器重新加载不起作用,我可以看到我在浏览器中所做的更改的唯一方法是每次保存时运行 npm 运行构建。

I can't get my head around the solution.我无法理解解决方案。 From what I understand, the issue is that when I run the script, webpack is still thinking that I'm in production mode and not in development.据我了解,问题是当我运行脚本时,webpack 仍然认为我处于生产模式而不是开发模式。 Other idea is that webpack gets stuck and is unable to clear the memory with the new bundle file in memory.其他想法是 webpack 卡住并且无法使用 memory 中的新捆绑文件清除 memory。 Below are my webpack.config.js and package.json files.下面是我的 webpack.config.js 和 package.json 文件。

Thanks!谢谢!

 // package.json { "name": "building-an-api", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "backend": "nodemon backend/server.js", "seed": "node backend/seed.js", "frontend": "webpack-dev-server --mode=development --env.NODE_ENV=local", "serve:frontend": "concurrently webpack-dev-server", "build": "webpack -p --mode=production --env.NODE_ENV=production", "mongo": "mongod --dbpath ~/data/db", "start": "node backend/server.js" }, "author": "", "license": "ISC", "devDependencies": { "@babel/core": "^7.10.2", "@babel/plugin-transform-runtime": "^7.10.1", "@babel/preset-env": "^7.10.2", "@babel/preset-react": "^7.10.1", "babel-loader": "^8.0.5", "bulma": "^0.9.1", "concurrently": "^6.0.0", "css-loader": "^3.5.3", "dotenv-webpack": "^4.0.0", "html-webpack-plugin": "^4.3.0", "node-sass": "^4.14.1", "sass-loader": "^8.0.2", "style-loader": "^1.2.1", "webpack": "^4.44.2", "webpack-cli": "^3.3.11", "webpack-dev-server": "^3.11.0", "webserver": "^4.0.2" }, "dependencies": { "@fortawesome/fontawesome-free": "^5.15.1", "@fortawesome/fontawesome-svg-core": "^1.2.32", "@fortawesome/free-solid-svg-icons": "^5.15.1", "@fortawesome/react-fontawesome": "^0.1.12", "axios": "^0.21.0", "bcrypt": "^5.0.0", "body-parser": "^1.19.0", "buefy": "^0.9.4", "bulma-calendar": "^6.0.2", "cloudinary-core": "^2.11.3", "cloudinary-react": "^1.6.8", "dotenv": "^8.2.0", "express": "^4.17.1", "file-loader": "^6.2.0", "jsonwebtoken": "^8.5.1", "mongoose": "^5.10.11", "mongoose-hidden": "^1.9.0", "mongoose-unique-validator": "^2.0.3", "react": "^16.13.1", "react-datepicker": "^3.3.0", "react-dom": "^16.13.1", "react-geocode": "^0.2.2", "react-map-gl": "^5.2.10", "react-rater": "^5.1.1", "react-router-dom": "^5.2.0", "react-select": "^3.1.0", "use-position": "0.0.8" } }

 // webpack.config.js const path = require('path') const webpack = require('webpack') const HtmlWebpackPlugin = require('html-webpack-plugin') const DotEnv = require('dotenv-webpack') const env = process.env.NODE_ENV === 'production'? new webpack.EnvironmentPlugin({...process.env }): new DotEnv() module.exports = () => { return { entry: './frontend/index.js', output: { filename: 'bundle.js', path: path.resolve('./backend/dist'), publicPath: '/' }, // target: 'web', devtool: 'source-map', module: { rules: [ { test: /\.js$/, use: 'babel-loader', exclude: /node_modules/ }, { test: /\.css$/, use: ['style-loader', 'css-loader'] }, { test: /\.s(a|c)ss$/, use: ['style-loader', 'css-loader', 'sass-loader'] }, { test: /\.(png|jpe?g|gif)$/i, use: 'file-loader' } ] }, devServer: { contentBase: path.resolve('frontend'), hot: true, open: true, port: 8000, watchContentBase: true, historyApiFallback: true, proxy: { '/api': { target: 'http://localhost:8000', secure: false } } }, plugins: [ new DotEnv(), new webpack.HotModuleReplacementPlugin(), new webpack.ProvidePlugin({ React: 'react' }), new HtmlWebpackPlugin({ template: 'frontend/index.html', filename: 'index.html', inject: 'body' }), env ] } }

I've found the solution, by comparing my code from before the deployment to Heroku.通过将部署前的代码与 Heroku 进行比较,我找到了解决方案。

I remembered that while I was developing the application, the API was using a proxy to http://localhost:8000, therefore the devserver needed a different port 8001.我记得在开发应用程序时,API 正在使用 http://localhost:8000 的代理,因此开发服务器需要不同的端口 8001。

Now the automatic build and browser reload work when I save changes in my app during development.现在,当我在开发期间保存我的应用程序中的更改时,自动构建和浏览器重新加载工作。

 devServer: { contentBase: path.resolve('frontend'), hot: true, open: true, port: 8001, watchContentBase: true, historyApiFallback: true, proxy: { '/api': { target: 'http://localhost:8000', secure: false } } },

You need to run it in watch mode.您需要在手表模式下运行它。 Try adding --watch in your package.json like "frontend": "webpack-dev-server --mode=development --watch --env.NODE_ENV=local".尝试在您的 package.json 中添加 --watch,例如“frontend”:“webpack-dev-server --mode=development --watch --env.NODE_ENV=local”。 It should work.它应该工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM