I'm making MERN (with React in Front-end and Express in Back-end) based application from a scratch. And I want webpack to reload page every time I save files. But it's not working.
My webpack.config.js
const path = require('path');
const entryFile = path.resolve(__dirname, 'src', 'client', 'app.js');
const outputDir = path.resolve(__dirname, 'public');
module.exports = {
entry: ['babel-polyfill', entryFile],
output: {
filename: 'bundle.js',
path: outputDir
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(scss|css)$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader',
},
{
loader: 'sass-loader'
}
]
}
]
}
};
my package.json
{
...
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "babel-node src/server/app.js",
"prestart": "webpack --mode development"
},
"repository": {
"type": "git",
"url": "git+https://github.com/2u4u/share-book.git"
},
...
"dependencies": {
"express": "^4.16.3",
"webpack": "^4.17.1",
"webpack-dev-server": "^3.1.5"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^1.0.0",
"node-sass": "^4.9.3",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"sass-loader": "^7.1.0",
"style-loader": "^0.22.1",
"webpack-cli": "^3.1.0",
"webpack-command": "^0.4.1"
}
}
I tried to add watch: true in the end of webpack config, but it's not starting server then. Where is the problem?
UPD: Can problem be in server/app.js?
import express from 'express';
import path from 'path';
const app = express();
const publicPath = path.resolve(__dirname, '..', '..', 'public');
app.use(express.static(publicPath));
app.listen(3000, () => {
console.log(`NEW one MERN Boilerplate listening on port 3000 and looking in folder ${publicPath}`);
});
Add -- hot in your dev script it should work if you’re using webpack v4. Otherwise you can add hot module in your webpack config.
The following should be your devDependencies
"dependencies": {
"webpack": "^4.17.1",
"webpack-dev-server": "^3.1.5"
},
and make changes in your "start and "prestart" script:
"prestart": webpack-dev-server --mode development --config webpack.config.js --open --hot
"start": "babel-node nodemon src/server/app.js",
You can directly add watch to prestart script in package.json
"prestart": "webpack --mode development" --watch --hot
OR
add devserver in webpack.config.js file something like below
devServer: {
contentBase: './public',
hot: true,
watch: true
}
"prestart": "webpack --mode development --watch"
Simple and easy. This has to be ran into one terminal, the server in another one.
Related
I have a website that I've had up for maybe a couple of years now, and I think it's starting to get outdated. I haven't touched it in a while, so I'm not exactly sure what is going on with it. I'm not exactly sure what the issue is, but it might have to do with my webpack version, or maybe my react version, I'm not really sure.
The issue started because I was trying to run webpack --mode production because I wanted to remake the bundle.js file, and when I would run this command, the file wouldn't create the bundle.js. Eventually I somehow came to the conclusion that there was something wrong with my webpack version, and so I updated it to the most recent version that was compatible with my dependencies which was version 4.46.0. I tried the command again, and this time it gives me this error:
[0] ./src/index.js 666 bytes {0} [built] [failed] [1 error]
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: Unexpected token (6:4)
4 |
5 | ReactDOM.render(
> 6 | <App />,
| ^
7 | document.getElementById("root")
8 | );
I tried reinstalling different versions of babel-loader, but the least bad error seems to be with the version 7.1.5. I also tried deleting my node-modules and then running npm install. Here is my package.json:
{
"name": "mywebsite",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --mode production",
"set-dev-server": "webpack-dev-server --open --mode development",
"start": "npm run build && npm run set-dev-server",
"deploy": "gh-pages -d dist",
"publish-demo": "npm run build && npm run deploy"
},
"repository": {
"type": "git",
"url": "git+my/github/link"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "my/github/link/issues"
},
"homepage": "my/github/link",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"file-loader": "^1.1.11",
"gh-pages": "^1.2.0",
"html-webpack-plugin": "^3.2.0",
"image-webpack-loader": "^7.0.1",
"jquery": "^3.5.1",
"node-css": "^0.1.0",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"sass-loader": "^7.3.1",
"style-loader": "^0.21.0",
"styled-components": "^5.2.1",
"webpack-cli": "^4.5.0",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"#babel/core": "^7.13.8",
"babel-loader": "^7.1.5",
"css-loader": "^5.1.1",
"node-sass": "^5.0.0",
"schema-utils": "^3.0.0",
"webpack": "^4.46.0"
}
}
From what I found online the issue could also have to do with the webpack.config.js, but all of the issues I could find were from really old versions of webpack (2 or 3?), but anyways here is what I have in that file:
const path = require("path");
const webpack = require("webpack");
const bundlePath = path.resolve(__dirname, "dist/");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const htmlWebpackPlugin = new HtmlWebpackPlugin({
template: path.join(__dirname, "dist/index.html"),
filename: "index.html"
});
module.exports = {
entry: path.join(__dirname, "src/index.js"),
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
options: { presets: ['env'] }
},
{
test: /\.scss$/,
use: [ 'style-loader', 'css-loader', 'sass-loader' ]
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: [
'file-loader',
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true, // webpack#1.x
disable: true, // webpack#2.x and newer
},
},
],
}
]
},
resolve: { extensions: ['*', '.js', '.jsx'] },
output: {
publicPath: bundlePath,
filename: "bundle.js"
},
devServer: {
contentBase: path.join(__dirname,'dist/'),
port: 3000,
publicPath: "http://localhost:3000/"
},
plugins: [ new webpack.HotModuleReplacementPlugin(), htmlWebpackPlugin ]
};
Looks like you are using a jsx token <> while your file is named index.js. Try naming it as index.jsx
I am trying to create a react app using webpack and babel,but when i actually start webpack dev server,using yarn run start,it shows the following error:-
i am using babel-loader version 8.1.0 and #babel/core version 7.10.2
My package.json:-
{
"name": "reactTemplate",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "webpack-dev-server --mode development --open --hot",
"build": "webpack --mode production"
},
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"devDependencies": {
"babel-loader": "^8.1.0",
"#babel/core": "^7.10.2",
"#babel/preset-env": "^7.10.2",
"#babel/preset-react": "^7.10.1",
"html-webpack-plugin": "^4.3.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.11.0"
}
}
My .babelrc:-
{
"presets": ["#babel/preset-env", "#babel/preset-react"]
}
My webpack.config.js:-
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index',
output: {
path: path.join(__dirname, './dist'),
filename: 'index_bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
}),
],
};
If you are running a very simple hello world example and extension is js then your settings seem fine. If you are using jsx files as well the add this to the webpack
test: /\.(js|jsx)$/,
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
I am trying to tidy some React code by using a static property of the class something like
class X extends React.Component {
static counter = 0
...
render() {
return( <div>{X.counter}</div>)
}
}
The build fails with unexpected token static counter = 0
My .babelrc is
{
"plugins": ["transform-class-properties", "transform-react-jsx" ]
}
and my package.json (using Webpack to build) is ...
{
"name": "js",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-latest": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"webpack": "^4.12.2",
"webpack-dev-server": "^3.1.4"
},
"scripts": {
"dev": "webpack-dev-server --inline --hot --no-info --env dev",
"build:prod": "webpack --env prod --progress --profile --colors",
"build:dev": "webpack --env dev --progres --profile --colors"
},
"dependencies": {
"css-loader": "^1.0.0",
"react-meter-bar": "^1.0.2",
"react-modal": "^3.5.1",
"react-websocket": "^2.0.0",
"style-loader": "^0.21.0",
"webpack-cli": "^3.0.8"
}
}
Can anyone help ?
I solved the issue by adding the plugin to the Webpack.config.js and using babel-loader - this is probably due to my lack of knowledge how Webpack works ...
const path = require('path');
module.exports = {
mode: 'development',
context: path.join(__dirname, 'src'),
entry: [
'./app.js',
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets:['react'],
plugins: ["transform-class-properties"]
}
}
],
},
{
test:/\.css$/,
use:['style-loader','css-loader'],
},
],
},
resolve: {
modules: [
path.join(__dirname, 'node_modules'),
],
},
};
I am trying to create my very first node package, it was successfully publish and live on npmjs.com website but whenever i include it on a sample react project using create-react-app i get this following error
Here's how i import it
import IconTooltip from 'react-icons-tooltip';
Here's the error
Module not found: Can't resolve './react-icons-tooltip' in '/home/ubuntu/workspace/lasting/src/Components'
This is the Package package.json file
{
"name": "react-icons-tooltip",
"version": "1.0.4",
"description": "",
"main": "./lib/IconTooltip.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --progress --colors --p",
"transpile": "node_modules/.bin/babel ./src/index.js --presets babel-preset-es2015 --out-file lib/IconTooltip.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.3",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"webpack": "^4.0.1",
"webpack-cli": "^2.0.10"
},
"dependencies": {
"prop-types": "^15.6.1",
"react": "^16.2.0",
"react-dom": "^16.2.0"
}
}
webpack.config.js
const webpack = require("webpack");
module.exports = {
entry: './src/index.js',
output: {
path: __dirname + '/build',
filename: 'IconToolTip.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.css$/,
loaders: ['style-loader', 'css-loader']
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
})
]
};
If i import the local the package seems to work fine
import IconTooltip from './lib/IconTooltip';
TIA!
I had that same problem before and that fixed it. I checked other modules and they all enter in index.js, is all I can say :)