Stuck on Setting Up Webpack with React - reactjs

I've tried looking at the docs and searching online, but I'm having a really hard time configuring webpack with my React project. I'm trying to use webpack-dev-server. When I do the yarn start command, this is what I see in the browser.
Image of what I see in the browser
I have no idea where to go from here. This is what my webpack.config.js looks like.
const path = require("path");
module.exports = {
entry: './src/components/app.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
devServer: {
contentBase: "./dist"
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
}
]
}
};
Here is my package.json
{
"name": "react-portfolio",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router-dom": "^4.2.2",
"react-scripts": "1.1.1",
"source-map-explorer": "^1.5.0"
},
"scripts": {
"analyze": "source-map-explorer build/static/js/main.*",
"start": "webpack-dev-server --open",
"build": "webpack",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.3",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"webpack": "^4.0.1",
"webpack-cli": "^2.0.9",
"webpack-dev-server": "^3.1.0"
}
}
Any guidance would be greatly appreciated.

Related

Cannot find module 'babel-preset-es2015' on yarn start

I got some errors when run yarn start in ReactJS app, anyone can help please?
ERROR in ./main.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module 'babel-preset-es2015'
Belows are the information of config files:
The package.json:
{
"name": "helloworld",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "webpack serve --mode development --open --hot",
"build": "webpack --mode production"
},
"dependencies": {
"#babel/core": "^7.13.10",
"#babel/preset-env": "^7.13.10",
"#babel/preset-es2015": "^7.0.0-beta.53",
"#babel/preset-react": "^7.12.13",
"babel-loader": "^8.2.2",
"html-webpack-plugin": "^5.3.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"webpack": "^5.25.0",
"webpack-cli": "^4.5.0",
"webpack-dev-server": "^3.11.2"
},
"devDependencies": {
"#webpack-cli/serve": "^1.3.0"
}
}
And the .babelrc:
{
"presets": [
"#babel/env",
"#babel/react"
]
}
After some research I got my answer: Edit the presets in webpack.config.js file the same as .babelrc file
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: { // <-- Sometimes you should change it from query to options to avoid error
presets: ['#babel/env', '#babel/react'] // <-- Edit here
}
}
]
},

How do we run react js production code on local using webpack?

I'm trying to run my ReactJS production code in local using webpack.
Can anyone check on my setup, please?
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: path.join(__dirname,'src','index.js'),
output: {
path: path.join(__dirname,'dist'),
filename: 'index.bundle.js'
},
mode: process.env.NODE_ENV || 'development',
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules']
},
devServer: {
contentBase: path.join(__dirname,'src'),
//host: '00.00.00.0',//your ip address
port: 4201,
},
module: {
rules: [
{
test: /\.jsx?$/,
// we do not want anything from node_modules to be compiled
exclude: /node_modules/,
use: ['babel-loader']
},
{
test: /\.html$/,
use: [{loader: "html-loader"}]
},
{
test: /\.(css|scss)$/,
use: [
"style-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS, using Node Sass by default
]
},
{
test: /\.json$/, loader: "json-loader"
},
{
test: /\.(ttf|eot|woff|woff2|jpg|jpeg|png|gif|mp3|svg|ico)$/,
loaders: ['file-loader']
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname,'src','index.html')
})
]
};
when I run npm run build, the process appears to execute correctly (creates build folder, which contains the bundled js file and the index.html file)
package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "babel-node ./node_modules/webpack/bin/webpack",
"_start": "babel-node ./node_modules/webpack-dev-server/bin/webpack-dev-server --open",
"start": "webpack-dev-server --mode development",
"build": "webpack --mode production"
},
"keywords": [],
"devDependencies": {
"#babel/core": "^7.4.0",
"#babel/node": "^7.2.2",
"#babel/plugin-proposal-class-properties": "^7.4.0",
"#babel/plugin-syntax-dynamic-import": "^7.2.0",
"#babel/preset-env": "^7.4.2",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"css-loader": "^2.1.1",
"file-loader": "^3.0.1",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.11.0",
"path": "^0.12.7",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1"
},
"dependencies": {
"#coreui/coreui": "^2.1.12",
"#coreui/coreui-plugin-chartjs-custom-tooltips": "^1.3.1",
"#coreui/icons": "0.3.0",
"#coreui/react": "^2.5.1",
"bootstrap": "^4.3.1",
"moment": "^2.24.0",
"node-sass": "^4.12.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-config": "^5.0.1",
"react-router-dom": "^5.0.1",,
"underscore": "^1.9.1"
}
}
I'm new to react and webpack. I have a very simple react project and tried to run it using webpack in local. But unable to resolve.
Try running NODE_ENV=product yarn start, I don't know what scripts you have in your packages.json, but setting the NODE_ENV=production before running the script is what you need
edit:
In your case, I would change your packages.json. because by default your webpack will run in development mode if you don't change the NODE_ENV
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "babel-node ./node_modules/webpack/bin/webpack",
"_start": "babel-node ./node_modules/webpack-dev-server/bin/webpack-dev-server --open",
"start": "webpack-dev-server",
"build": "webpack --mode production"
},
Then this NODE_ENV=product yarn start will definitely work
I think you can add a script for production mode too as
"prod": "webpack-dev-server --mode production",
and then
yarn run prod
Hope it helps
You can also build the production build using yarn build & then deploy locally to test the production.
You can read more about how to test the production build locally at
https://medium.com/#samratshaw/test-react-production-build-locally-434907be9e49

cant use react - Unexcepted errors

i used react once and it's worked ok.
now i tried to install it again, and it's not working.
i'll show you my files.
that's webpack.config.js:
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
};
that`s .babelrc:
{
"presets": ["env", "react"]
}
and that's package.json
`{
"name": "shopit",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --mode production"
},
"author": "",
"license": "ISC",
"dependencies": {
"webpack": "^4.8.1",
"webpack-cli": "^2.1.3",
"webpack-dev-server": "^3.1.4"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"react": "^16.3.2",
"react-dom": "^16.3.2"
}
}
i got this error: Uncaught SyntaxError: Unexpected identifier
on the first line :
import React from 'react';
and that`s what my file has. only that file.
someone knows what to do ?

Bundle.js file isn't getting generated by webpack for production build

I'm unable to generate the bundle.js file.
This is my webpack.config.js.
module.exports = {
entry: [
'./src/index.js'
],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
loaders: [{
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['react', 'es2015', 'stage-1']
}
}]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './'
}
};
This is my package.json
{
"name": "redux-react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
"test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
"test:watch": "npm run test -- --watch"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"chai": "^3.5.0",
"chai-jquery": "^2.0.0",
"jquery": "^2.2.1",
"jsdom": "^8.1.0",
"mocha": "^2.4.5",
"react-addons-test-utils": "^0.14.7",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"babel-preset-stage-1": "^6.1.18",
"lodash": "^3.10.1",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"react-redux": "4.3.0",
"react-router": "^2.0.1",
"redux": "^3.0.4"
}
}
The main aim is to build a production-ready app hence how do I configure my webpack to generate the bundle.js file. Also is it necessary to have seperate webpack.config for dev and production server?
webpack-dev-server is for development purpose. This will not put your bundle.js in your work directory, it will compile your files in-memory.
I can see from your package scripts that you are running a webpack-dev-server to build the code

Module parse failed: Unexpected token using webpack-dev-server

I'm trying to follow along the examples in the Learning Redux book. Unfortunately the latest versions of webpack and babel have all changed and I updated to the latest versions which deviate from the configurations in the book.
I've read the latest documentation from both babel and webpack and I believe I have the correct .babelrc and config.webpack.js that is appropriate.
But I'm struggling to get jsx to compile with the webpack-dev-server. I get this error when running npm start:
ERROR in ./src/index.js
Module parse failed: Unexpected token (10:4)
You may need an appropriate loader to handle this file type.
|
| ReactDOM.render(
| <h1>hello world!</h1>,
| document.getElementById('root')
| )
# multi (webpack)-dev-server/client?http://localhost:8080 ./src
Here is my full setup: https://github.com/homanchou/learning_redux
package.json
{
"name": "learningredux",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"start": "webpack-dev-server --open --mode development",
"build": "webpack --mode production",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"eslint": "^4.19.1",
"eslint-plugin-react": "^7.7.0",
"webpack": "^4.6.0",
"webpack-cli": "^2.1.2",
"webpack-dev-server": "^3.1.3"
},
"dependencies": {
"npm": "^6.0.0",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"redux": "^4.0.0"
}
}
babelrc.
{
"presets": ["env", "react"],
"plugins": [ "transform-object-rest-spread" ]
}
config.webpack.js
const path = require('path')
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve('dist'),
filename: 'main.js'
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}]
}
}
Any help is appreciated.
You just mixed the order in the config.webpack.js filename.
It needs to be webpack.config.js
Or you need to run webpack with --config and pass the filename.
Everything else should work.

Resources