Web-pack successfully compiled but does not show output in browser - reactjs

Browser http://localhost:8080 still says
This page isn’t working
localhost didn’t send any data.
ERR_EMPTY_RESPONSE

As you are using webpack, please check what parameters you have used in devserver tag of your webpack.config.js file.
It should be as below:
devServer: {
inline: true,
contentBase: './src',
port: 3000
},
Hope this will resolve your issue.

Related

webpack 5 / webpack-dev-server: how to serve other file then index.html

When i was using webpack 4 (4.44.1) with webpack-dev-server (3.11.0) i had a setting
devServer: {
index: 'other_filename.html'
}
and thanks to that setting dev server was serving that file at / instead of index.html (which is not generated in the build process in my project). How can i get that result with latest versions (5.73.0 / 4.9.2) as index property is not valid anymore (get a build error)? I can't really see any entry that would match that setting in latest docs.
Thanks in advance
I found it:
devServer: {
devMiddleware: {
index: 'filename.html',
},
},

PM2 serve SPA build folder. It works but has tons of errors like "Error while serving ... ENOENT: no such file or directory..."

The problem is that the logs are flooded, but it works fine.
The build folder contains the build from React using CRA (Create React App).
So from the PM2 Docs I have this:
ecosystem.config.js
module.exports = {
name: "projectName",
script: "serve",
watch: true,
env: {
NODE_ENV: "production",
PM2_SERVE_PATH: './build',
PM2_SERVE_PORT: 5001,
PM2_SERVE_SPA: 'true',
PM2_SERVE_HOMEPAGE: './index.html'
},
}
I'm using PM2 serve command.
Command to start PM2 process:
pm2 start
Errors
Only happens when I enter for the first time or when I reload.
Error while serving /.../projectName/build/routeX with content-type text/plain : ENOENT: no such file or directory, open '/.../projectName/build/routeX'
I think you have to find the nature of those requests leading to an error. If they are originated from your app, then you have either to put away what causes that request or let something to be in that place inside of your build folder (depends on request's reason). If they are originated not from your app, then you should figure out where they come from. "Network" Tab of DevTools should help you in both cases
This issue is fixed in pm2 v5.2.0
https://github.com/Unitech/pm2/pull/5272

How is this webpack devServer configured incorrectly?

I am running into issues with my webpack-dev-server. When i try to load at a specified route such as "http://localhost:3000/login" I get the following message "Cannot GET /login".
I've read through the docs and seen many different solutions, but I don't see the flaws in my config. I have my webpack setup like this.
webpack.config.js
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js',
},
module: [ rules... ],
devServer: {
contentBase: path.join(__dirname, 'build'),
compress: true,
port: 3000
},
}
My scripts for the app look like this:
"build": "webpack",
"dev": "webpack-dev-server --open"
If I run dev the app will open to the initial route - "/". If I click on a link to login - "/login" that will load fine. But if I refresh the page on login it will throw the "Cannot GET /login" error.
Is there something I'm missing or have wrong in my webpack configuration?
webpack-dev-server only serves two things: bundled assets, and whatever is in the directory you gave it as the value for contentBase. things work the way you want when you browse to / then navigate to /login since I assume you have some kind of client-side routing framework in your webapp. none of that applies when you refresh your browser and ask the server for a page at /login .
w-d-s is 404ing on you since you don't have a file named "login" in your build directory (and I doubt you have a compiled bundle or chunk with that name either). do you have an index.html file in that directory? is that what you're served when you visit localhost:3000/ ? does that file have a <script> tag which loads your webapp?
what were you expecting to happen when you visited /login ? I'm assuming you wanted to display your webapp's login screen when you visit localhost:3000/login ? if so you'll most likely need to use a separate server like express.js to server-side-render your webapp. this server can either be on a different port from the server launched by w-d-s, or it can actually use w-d-s as middleware so you only need one server with one port.
this seems like a decent resource at first glance: Server side rendering with react, react-router, and express . but if it doesn't work for you, there are dozens more on SO already which might.

Serve multiple entry points with webpack dev server

I've got multiple entry points being generated with outputs successfully but what I'm not too sure on is if there is a way with the webpack dev server to serve these multiple entry points and somehow be able to work on them all at the same time?
Here is my basic setup right now, is there a way I can get webpack dev server to somehow serve these on different ports maybe or by changing the URL serve the different entry points?
I've tried changing the port number on one of the apps but it doesn't seem to run a server with that port, which I kind of figured it wouldn't
entry: {
dashboard: [
"webpack-dev-server/client?http://localhost:3000",
"webpack/hot/only-dev-server",
paths.appIndexJs
],
display: [
"webpack-dev-server/client?http://localhost:3000",
"webpack/hot/only-dev-server",
paths.displayJs
]
},
output: {
pathinfo: true,
filename: "static/js/[name].js",
chunkFilename: "static/js/[name].chunk.js",
publicPath: publicPath,
devtoolModuleFilenameTemplate: info =>
path.resolve(info.absoluteResourcePath).replace(/\\/g, "/")
},

Configuring the Webpack Dev Server for Angular html5mode

I'm trying to enable html5mode in an Angular 1 application. This mode requires the webserver to return the same HTML for various paths. In nginx, this is done by adding a try ... index.html for the location, meaning nginx always fails back to the main html if it can't find the page.
However, while development I have no nginx, I'm using the Webpack dev server to serve the pages, with a proxy to the backend. How do I configure the Webpack dev server to behave in a similar fashion?
Oh dear. Oh dear oh dear oh dear.
I scanned the documentation twice, and only after posting this question I found the historyApiFallback settings, as described here.
Add this to the settings:
devServer: {
historyApiFallback: true,
proxy: { ... }
}
I'm keeping this question here hoping it will help others save time in the future.

Resources