webpack configuration error - reactjs

I tried to configure web pack for reacts web app, it kept telling me to use
npm install -D webpack-cli :
The CLI moved into a separate package: webpack-cli.
Please install 'webpack-cli' in addition to webpack itself to use the CLI.
-> When using npm: npm install webpack-cli -D
-> When using yarn: yarn add webpack-cli -D
module.js:557
throw err;
^
when I did what it was asking me it gave me this error :
C:\Users\Desktop\lazabre1>webpack-dev-server
C:\Users\Desktop\lazabre1\webpack.config.js:59
new webpack.NoErrorsPlugin()
^
this is my webpack.config.js file :
var webpack = require('webpack');
var path = require('path');
module.exports = {
devtool: 'inline-source-map',
entry: [
'webpack-dev-server/client?http://127.0.0.1:8080/',
'webpack/hot/only-dev-server',
'./src'
],
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
resolve: {
modulesDirectories: ['node_modules', 'src'],
extensions: ['', '.js']
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loaders: ['react-hot', 'babel?presets[]=react,presets[]=es2015']
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
]
};
I have 2 questions: first - how to solve this problem //
second - what is the difference between npm install webpack and npm install webpack-cli.

You are using a configuration file written for Webpack 1 (I guess, by the use of the module.loader key and the NoErrorsPlugin) and using Webpack 4 (if you did not specify otherwise, and by the request to use the CLI which is not included by default anymore).
Upgrade your configuration by following the documentation and update the NoErrorsPlugin.
The webpack-cli is a package to use webpack from the command-line tool (what you are trying to do).

Related

Webpack unable to find babel-loader

I'm having a bit of trouble adding react to a legacy application. While I found plenty of materials on getting started, I ran into a bunch of issues related to my particular configuration and exacerbated by my utter lack of webpack knowledge (so much time relying on react cli tools makes one lax).
My configuration:
./docs/jslib = my node_modules, that's where yarn installs modules
./docs/jscripts = my general js folder, various js scripts go there
./docs/jscripts/mini = my dist folder, some things get built/packed by gulp and end up here. This is where the built things are expected to go
./docs/jscripts/react(/react.js) = my desired root for react things, where the react.js is the entrypoint for DOM selection and rendering (at least for the first attempt).
My webpack config:
const path = require("path");
module.exports = {
entry: "./docs/jscript/react/react.js",
output: {
path: path.resolve(__dirname, "./docs/jscript/mini"),
filename: "react.js"
},
resolve: {
extensions: [".js", ".jsx"],
modules: [
path.resolve(__dirname, "./docs/jslib")
],
alias: {
'babel-loader': path.resolve(__dirname, "./docs/jslib/babel-loader")
}
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
include: [
path.resolve(__dirname, "./docs/jscript/react")
],
exclude: [
path.resolve(__dirname, "./docs/jslib")
],
use: {
loader: "babel-loader"
}
}
]
}
};
The error I get on running webpack:
yarn run v1.21.1
$ ./docs/jslib/babel-loader/node_modules/.bin/webpack --mode production
Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.
Hash: 210250af48f3cf84fa4a
Version: webpack 4.41.6
Time: 75ms
Built at: 02/14/2020 9:23:09 AM
ERROR in Entry module not found: Error: Can't resolve 'babel-loader' in '/var/www/html'
I have webpack-cli, I can run webpack straight, I can run it from the modules bin folder, it's all the same problem - it can't find babel-loader even though babel-loader brings its own webpack script which I use, so the loader is obviously there in the "node_modules" that's actually on a different path.
From what I gathered, all I had to do was to add my custom node_modules path to the webpack config under the resolve settings, which has solved my initial errors related to packages not found (yet webpack still can't find the loader).
Note: if I symlink my ./docs/jslib as ./node_modules, it works as expected.
Is there something I'm missing?
Thanks!
After some digging, I found a solution and some details that other might want to keep in mind when configuring webpack in a rather non-standard context:
resolve: points to modules that are directly required in various scripts
resolveLoader: should point to the same, for the purpose of accessing loaders (which is what I needed)
Solution:
resolveLoader: {
extensions: ['.js', '.json', '.jsx'],
modules: [
path.resolve(__dirname, "./docs/jslib")
],
mainFields: ['loader', 'main']
}

ERROR in Entry module not found: Error: Can't resolve './src' in

I am trying to add react components to angular1.5 project.
I added to my wepback the path of first component this isnt starting with index.js its just jsx react component.
when I try to run my build I am getting error
WARNING in configuration The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode'
option to 'development' or 'production' to enable defaults for each
environment. You can also set it to 'none' to disable any default
behavior. Learn more: https://webpack.js.org/configuration/mode/
ERROR in Entry module not found: Error: Can't resolve './src'
my code looks like
const path = require('path');
const config = require('./ENVIRONMENT_CONFIG_FOR_GULP/configConstants');
module.exports = {
entry: path.resolve('entryPoint.js'),
mode: 'development',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]},
output: {
filename: config.webpackBundleFileName,
path: path.resolve(config.src, 'modules')
},
devtool: "#source-map"
};
You have two issue in your configuration
First you havent set the mode of webpack yet
So to fix it add below script to your scripts section in package.json
"scripts": {
"prod": "webpack -p --mode=production",
"start": "webpack --mode=development"
}
then run npm run start or npm run prod
Second issue try to use string for your path in this config
path: path.resolve(config.src, 'modules')
change to something like this
output: {
path: path.resolve(__dirname, "wwwroot/dist"),
filename: "[name].js",
publicPath: "/dist/"
},
I find the issue the plugin that I used was changed due to update packages versions:
the current code
let compiler = webpack(webpackConfig);
new webpack.ProgressPlugin().apply(compiler);
return compiler.run(function (err, stats) {

Webpack cannot identify my JSX syntax inside my js file

When i try to bundle my modules with webpack, it cannot identify the JSX syntax inside my index.js file and gives the following error:
ERROR in ./src/index.js 29:3
Module parse failed: Unexpected token (29:3)
You may need an appropriate loader to handle this file type.
| const searchTerm = _.debounce(term => {this.searchTerm(term)}, 300);
| return (
> <div>
| <SearchBar onInputChange= {searchTerm}/>
| <div className="row">
# multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.js main[1]
This is my webpack config:
const Path = require('path');
module.exports = {
entry: ['./src/index.js'],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
include: Path.resolve(__dirname, '../src'),
use: {
loader: 'babel-loader',
options: {
presets: ["#babel/preset-react", "#babel/preset-env"]
}
}
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [ 'css-loader' ]
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './',
watchOptions: {
aggregateTimeout: 300,
poll: 1000
}
}
};
I have added the babel presets for react namely #babel/preset-react. I have also added the babel-loader but still it cannot identify the JSX syntax.
I cloned your repo and found a bunch of conflicts on your code setup.
Some of your packages are not aligned with the latest versions of your #babel/core package. If you are going to use the latest version of babel, might as well use the latest presets.
`
devDependencies: {
#babel/cli: ^7.0.0,
#babel/core: ^7.0.0,
#babel/preset-env: ^7.0.0,
#babel/preset-react: ^7.0.0,
babel-loader: ^8.0.0"
}
`
and I removed babel-preset-env and babel-preset-react (the old versions) on your dependencies.
Pick one babel configuration. It's either on your package.json or on .babelrc. I suggest you stick with the .babelrc file. And changed the values of the presets property.
`"presets": ["#babel/preset-react", "#babel/preset-env"],`
On your webpack.config.js you don't need to do include: include: Path.resolve(__dirname, './src'). You can also remove this line:
`presets: ["react", "env"]`
and BTW, on your package.json,
"scripts": {
- "start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
+ "start": "webpack-dev-server --mode development",
...
when you run npm start your project will look into a locally installed webpack-dev-server and use the global one if it does not find the local package.
Hope this helps. :)

Whitescreen of death after pulling from git repo (ReactJS, Nginx)

Whenever I perform a git pull from my master branch onto my server, all my React files seem to just disappear and the screen turns white.
The temporary workarounds I had found were:
Delete browser cookies, cache & site history, and then close the browser and try again.
Delete node_modules, npm install all react dependencies again
After a while, the site reappears and everything works as normal until the next time after a few pull requests, the problem appears again.
Any console I use on any browser shows no error messages at all.
After 2+ weeks of googling around, I can't seem to find anything that relates to this issue.
Here are my specs:
Ubuntu 16.04 server
Framework: React 16.2.0
webpack 1.12
nginx version: nginx/1.10.3 (Ubuntu)
git version 2.7.4
My webpack settings (for clarity, I compile all my react files with the command):
node_modules/.bin/webpack --config webpack.local.config.js
(local)
var path = require("path")
var webpack = require('webpack')
var BundleTracker = require('webpack-bundle-tracker')
var config = require('./webpack.base.config.js')
config.devtool = "#eval-source-map"
config.plugins = config.plugins.concat([
new BundleTracker({
filename: './webpack-stats-local.json'
}),
])
config.module.loaders.push({
test: /\.js[x]?$/,
exclude: /node_modules/,
loaders: ['react-hot-loader/webpack', 'babel'],
})
module.exports = config
(base)
var path = require("path")
var webpack = require('webpack')
module.exports = {
context: __dirname,
entry: {
App1: './path/to/App1/',
App2: './path/to/App2/',
// ...
App10: './path/to/App10/',
vendors: ['react'],
},
output: {
path: path.resolve('./backend/static/bundles/local/'),
filename: "[name]-[hash].js"
},
externals: {
"gettext":"gettext",
"django":"django",
}, // add all vendor libs
plugins: [
new webpack.optimize.CommonsChunkPlugin('vendors', 'vendors.js'),
],
module: {
loaders: []
},
resolve: {
modulesDirectories: ['node_modules', 'bower_components'],
extensions: ['', '.js', '.jsx']
},
}
Any help will be greatly appreciated
I solved this problem by updating Webpack to version 4 + updating the dependencies i used while getting rid of the ones i don't use.

Webpack / Babel / React build error: "Unknown option: foo/node_modules/react/react.js.Children"

I'm trying to build a project with webpack and react with this webpack config:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: [
'babel-polyfill',
'./app/less/main.less',
'./app/main.js',
'webpack-dev-server/client?http://localhost:8080'
],
output: {
publicPath: '/',
filename: 'dist/main.js'
},
debug: true,
devtool: 'source-map',
module: {
loaders: [
{
test: /\.js$/,
include: path.join(__dirname, 'app'),
loader: 'babel-loader',
query: {
presets: ['react', 'es2015']
}
},
{
test: /\.less$/,
loader: "style!css!autoprefixer!less"
},
]
}
};
I am sure I have the needed pm modules too and have webpack installed, however when running webpack I get:
Module build failed: ReferenceError: [BABEL] /Users/me/foo/app/main.js: Unknown option: foo/node_modules/react/react.js.Children
Any ideas?
Sorry I forgot to install babel-preset-react:
$ npm install babel-preset-react --save-dev
I just wanted to add that I got the error after I uninstalled an old npm module I wasn't using in my project anymore. Which was weird because I wasn't using it anywhere - how could uninstalling something that isn't used anywhere cause an error?
Turns out that one of that modules sub-dependencies had babel-preset-react, which I had missed installing to my own project when I started it. Thus, uninstalling that package also uninstalled the critical babel-preset-react!
For over a year, my react app could compile thanks to another package's sub-dependency...
So yes, installing babel-preset-react solved the issue for me.

Resources