Here's my webpack-dev-server configuations
module.exports = {
entry: './main.js',
output: {
path: './',
filename: 'bundle.js'
},
devServer: {
inline: true,
port: 9000
},
module: {
loaders: [{
test: /\.js$/,
exclude: '/(node_modules)/',
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
}]
}
};
Why would I get the following error when I run the server
ERROR in (webpack)-dev-server/~/strip-ansi/index.js
Module build failed: Error: Couldn't find preset "react" relative to directory "/Users/ecco/.nvm/versions/node/v5.4.0/lib/node_modules/webpack-dev-server/node_modules/strip-ansi"
my package.json
"devDependencies": {
"babel-core": "^6.4.5",
"babel-loader": "^6.2.2",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"webpack": "^1.12.13"
}
You don't appear to have the webpack-dev-server installed as a local node module (it's not in your package.json
You can see which modules are installed locally and globally with npm list and npm list --global. To look for specific modules, pipe that command into grep i.e. npm list --global | grep webpack-dev-server
I'd suggest installing the dev server as a devDependency of your project. As a general rule, try to avoid installing modules with -g unless absolutely necessary. Also, use npm scripts (in your package.json) to run modules in your local directory, without having to reference them explicitly e.g.
{
"scripts": {
"serve": "webpack-dev-server --config your.config.js"
}
}
then run npm run serve rather than running ./node_modules/.bin/webpack-dev-server --config your.config.js (or worse, installing it globally)
Related
What I want to do
I have a React Typescript project and I want to setup a debug configuration in Rider, which fulfills the following criteria:
a dev server is started with hot reloading, so when I change files, the application gets updated automatically
a javascript debugger gets attached to the running application, so I can set breakpoints in the jsx files inside of Rider (not in Chrome DevTools) and the application actually stops at the breakpoint
I prefer to use webpack directly and not indirectly through create-react-app
What's working
I currently run my application on the webpack dev server with webpack serve for development purposes. The dev server is running and I can debug the typescript code in Chrome DevTools successfully as you can see here.
The problem
As mentioned above, now I want to attach a debugger from inside Rider, so I can set breakpoints directly in my IDE - and that's where I failed.
What I've tried
In the Jetbrains documentation for debugging a webpack application (https://www.jetbrains.com/help/rider/Using_Webpack.html#debug_application_that_uses_webpack) I was told that the debugging should work the same as for React applications that were setup with create-react-app. So I followed the instructions here: https://blog.jetbrains.com/webstorm/2017/01/debugging-react-apps/?_ga=2.129883279.2614435.1634130690-1852059688.1626293073.
I ran yarn start from within the run configuration in debug mode. This executed webpack serve as defined in my package.json.
The page was available at http://localhost:9000 and I got the following output in the process console:
/usr/local/bin/node /usr/local/lib/node_modules/npm/bin/npm-cli.js run start --scripts-prepend-node-path=auto
Debugger listening on ws://127.0.0.1:42631/294d3b20-969f-486e-917e-22c6350d23e4
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
> applicationName#1.0.0 start
> webpack serve
Debugger listening on ws://127.0.0.1:33159/3b5cb2c1-674a-4d9c-888f-b3bdf6f3d3a6
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:9000/
[...]
However, if I ctrl+shift+click on the http://localhost:9000/ link from the console, I get the following error.
My client app folder is located inside of a sub folder of a C# project (SolutionName/AppliationName/ClientApp), which I use as a backend. Can this maybe cause the problem?
I'm kinda stuck here, so I'm happy for all help. :)
Below you will find further information about my system and the relevant files.
My environment
OS: Ubuntu 20.04.2
IDE: Rider 2021.2.2
versions of the dependencies are listed in the package.json below
Files
package.json
{
[...]
"scripts": {
"start": "webpack serve",
"watch": "webpack --watch",
"build": "tsc && NODE_ENV=production webpack",
"build-dev": "webpack"
},
"dependencies": {
"#types/react": "^17.0.21",
"#types/react-dom": "^17.0.9",
"#types/typescript": "^2.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"#babel/core": "^7.15.8",
"#babel/preset-env": "^7.15.8",
"#babel/preset-react": "^7.14.5",
"#babel/preset-typescript": "^7.15.0",
"#webpack-cli/generators": "^2.4.0",
"webpack": "^5.58.1",
"webpack-cli": "^4.9.0",
"webpack-dev-server": "^4.3.1",
"workbox-webpack-plugin": "^6.3.0",
"babel-loader": "^8.2.2",
"css-loader": "^6.4.0",
"file-loader": "^6.2.0",
"style-loader": "^3.3.0",
"ts-loader": "^9.2.6",
"html-webpack-plugin": "^5.3.2",
"mini-css-extract-plugin": "^2.4.2",
"prettier": "^2.4.1",
"typescript": "^4.4.3"
}
}
webpack.config.js
const path = require("path")
const HtmlWebpackPlugin = require('html-webpack-plugin')
let mode = "development";
if (process.env.NODE_ENV === "production") {
mode = "production";
}
module.exports = {
mode: mode,
module: {
rules: [
{
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
cacheDirectory: true,
presets: [
["#babel/preset-env", { targets: { node: "8" } }],
"#babel/preset-typescript",
"#babel/preset-react"
]
}
}
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
},
]
},
plugins: [new HtmlWebpackPlugin({ template: './public/index.html' })],
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
extensions: [".tsx", ".ts", ".jsx", ".js", "..."],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
compress: true,
port: 9000,
open: true,
hot: true
},
// generate source maps to debug the initial source files
devtool: "source-map"
}
The reason for the problems was that chromium was installed with snap. This doesn't work, but if you install the chromium packages directly from the debian repository everything works perfectly.
More details here: https://rider-support.jetbrains.com/hc/en-us/community/posts/4409573673746-Javascript-Debugging-not-working-in-Rider
So I have been trying to setup React Js environment. I am facing the babel dependencies error. I know this problem is very common and there are tons of answers available but none of them have worked for me so far.
I have searched through the internet to solve it but it still shows the same error.
This is the error I am getting:
ERROR in ./main.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module 'babel-preset-es2015' from 'D:\my-app'
at Function.module.exports [as sync] (D:\my-app\node_modules\resolve\lib\sync.js:58:15)
at resolveStandardizedName (D:\my-app\node_modules\#babel\core\lib\config\files\plugins.js:101:31)
at resolvePreset (D:\my-app\node_modules\#babel\core\lib\config\files\plugins.js:58:10)
at loadPreset (D:\my-app\node_modules\#babel\core\lib\config\files\plugins.js:77:20)
at createDescriptor (D:\my-app\node_modules\#babel\core\lib\config\config-descriptors.js:154:9)
at items.map (D:\my-app\node_modules\#babel\core\lib\config\config-descriptors.js:109:50)
at Array.map (<anonymous>)
at createDescriptors (D:\my-app\node_modules\#babel\core\lib\config\config-descriptors.js:109:29)
at createPresetDescriptors (D:\my-app\node_modules\#babel\core\lib\config\config-descriptors.js:101:10)
at passPerPreset (D:\my-app\node_modules\#babel\core\lib\config\config-descriptors.js:58:96)
Child html-webpack-plugin for "index.html":
1 asset
Entrypoint undefined = index.html
[./node_modules/html-webpack-plugin/lib/loader.js!./index.html] 448 bytes {0} [built]
[./node_modules/lodash/lodash.js] 527 KiB {0} [built]
[./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {0} [built]
[./node_modules/webpac
This is my .babelrc
{
"presets": ["es2015"]
}
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './main.js',
output: {
path: path.join(__dirname, '/bundle'),
filename: 'index_bundle.js'
},
devServer: {
inline: true,
port: 8080
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
},
plugins:[
new HtmlWebpackPlugin({
template: './index.html'
})
]
}
package.json
{
"name": "my-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --mode development --open --hot",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"webpack-cli": "^3.3.2",
"webpack-dev-server": "^3.3.1"
},
"devDependencies": {
"#babel/core": "^7.4.4",
"#babel/preset-env": "^7.4.4",
"#babel/preset-es2015": "^7.0.0-beta.53",
"babel-loader": "^8.0.6",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.31.0"
}
}
babel-loader#8.x uses Babel 7.x, which is #babel/core#^7.0.0, and more importantly in your case #babel/preset-env#7 replaces babel-preset-env#^1.7.0.
You'll need to make sure to do
npm install #babel/core #babel/preset-env
and update your Babel config to use #babel/preset-env instead of babel-preset-env with something like
"presets": [
"#babel/preset-env"
]
Note: For others coming across this, the issue may also be that you're using plugins/preset from Babel 6 on Babel 7. This may be hard to notice if you're using a third-party Babel preset since the versions of the presets may not match the version of Babel itself.
For NPM, run
npm install -D babel-loader #babel/core #babel/preset-env webpack
FOR YARN, run
yarn add --dev babel-loader #babel/core #babel/preset-env webpack
Late but might be helpful.
Downgrade the version of #babel-loader to version 7.
It worked for me when i encountered same problem
If you noticed this after installing a package like web3.
Kill the server and try to rebuild hope it helps
I'm trying to create a Reactjs project from scratch. I've heard that it's easier to use create-react-app. I have also been told that the standard industry practise is to use webpack to bootstrap your React projects.
I have created my webpack config file like below:
const path = require('path');
const DIST_DIR = path.resolve(__dirname, "dist");
const SOURCE_DIR = path.resolve(__dirname, "src");
const config = {
entry: SOURCE_DIR + "/app/app.js",
output: {
path: DIST_DIR + '/app',
filename: 'bundle.js',
publicPath: '/app/'
},
module: {
loaders: [
{
test: "/\.js?/",
include: SOURCE_DIR,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-2']
}
}
]
}
};
module.exports = config;
and I have added the build and build:prod scripts for running in devt and production mode as so:
{
"name": "basics",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server",
"build": "webpack -d && copy src/index.html dist/index.html &&
webpack-dev-server --content-base: src/ --inline --hot",
"build:prod": "webpack -p && copy src/index.html dist/index.html"
},
"keywords": [
"React",
"JavaScript",
"Front End"
],
"author": "MIT",
"license": "ISC",
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"devDependencies": {
"#babel/preset-env": "^7.0.0-beta.40",
"#babel/preset-react": "^7.0.0-beta.40",
"#babel/preset-stage-2": "^7.0.0-beta.40",
"babel-loader": "^7.1.4",
"webpack": "^4.1.1",
"webpack-cli": "^2.0.10",
"webpack-dev-server": "^3.1.0"
}
}
When I run npm start, the app crashes and I get this error in console:
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration.module has an unknown property 'loaders'. These properties are valid:
object { exprContextCritical?, exprContextRecursive?, exprContextRegExp?,
exprContextRequest?, noParse?, rules?, defaultRules?,
unknownContextCritical?, unknownContextRecursive?,
unknownContextRegExp?, unknownContextRequest?, unsafeCache?,
wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp?,
strictExportPresence?, strictThisContextOnImports? }
-> Options affecting the normal modules (`NormalModuleFactory`).
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! basics#1.0.0 build: `webpack -d && cp src/index.html
dist/index.html && webpack-dev-server --content-base: src/ --inline --hot`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the basics#1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional
logging output above.
I think the error is in my build script where I'm doing the copy but I can't be sure as it's my first time with webpack.
Can someone help me solve this? Thanks.
You want to use rules instead of loaders.
The information is there in the error. It tells you what are valid properties.
From the webpack docs https://webpack.js.org/concepts/loaders/
module.exports = {
module: {
rules: [
{ test: /\.css$/, use: 'css-loader' },
{ test: /\.ts$/, use: 'ts-loader' }
]
}
};
So for yours:
module: {
rules: [
{
test: "/\.js?/",
include: SOURCE_DIR,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-2']
}
}
]
}
Create-react-app is the easiest option but if you really want to understand how everything is set up under the hood you might want to build a simple app using Webpack, Babel and React from scratch. that's what I did.
Or you could go through my tutorial to save some time. Webpack, Babel, React, Redux, Apollo. From scratch to the production stack.
Seems like a truly stupid question that must have an answer somewhere, but I've searched for hours to no avail. I'm new to ReactJS and building with Webpack, build configs in general. I'm using Webpack to link and bundle my entire project, including ReactJS. It works great, but I cannot figure out any way to get the bundle to come out un-minified so that I can debug issues when they arise.
Here is my Webpack config:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'public/js');
var APP_DIR = path.resolve(__dirname, 'build-source/js');
var config = {
entry: APP_DIR + '\\main.js',
output: {
path: BUILD_DIR,
filename: 'build.js' // want this output file to end un-minified
},
module: {
loaders: [
{
test: /\.jsx?/,
include: APP_DIR,
loader: 'babel'
}
]
}
};
module.exports = config;
I run the bundling execution with either npm run dev or npm run build which call upon the following from my package.json:
{
/* blah blah */,
"scripts": {
"start": "node ./bin/www",
"dev": "webpack -d --watch",
"build": "webpack -p"
},
"dependencies": {
"babel-core": "^6.16.0",
"babel-loader": "^6.2.5",
"babel-preset-react": "^6.16.0",
"body-parser": "~1.15.1",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"express": "~4.13.4",
"helmet": "^3.1.0",
"morgan": "~1.7.0",
"mysql": "^2.11.1",
"querystring": "^0.2.0",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"request": "^2.75.0",
"serve-favicon": "~2.3.0",
"webpack": "^1.13.2"
}
}
What do I need to change to get un-minified JavaScript bundles out of my Webpack execution?
Why don't simply use:
module: {
// ....
},
optimization: {
minimize: false
},
It is a late answer but maybe helps someone else.
When you set mode: 'development' you will get a non minified bundle.
Webpack is using terser plugin by default for javascript minification in production mode even if you don't use it explicitly.
Also for debugging devtool option is a must.
As an example :
const config = {
...
mode: development,
devtool: 'source-map',
...
};
When you use the -p flag for webpack's cli, you are telling webpack to use the UglifyJSPlugin (behind the scenes)
So instead, I would have a separate build task that runs webpack without the -p flag and instead passes the following in your config instead.
var webpack = require('webpack');
module.exports = {
plugins: [
new webpack.optimize.UglifyJsPlugin({
options: {
compress: {drop_debugger: false}
}
})
]
};
Additionally, you can see that I've passed a custom option to the UglifyJsPlugin (which just corresponds to UglifyJs's compression options).
You should add to your webpack.build.js file
devtool: 'inline-source-map',
I have a React project using Webpack and Babel. When I created it on an office computer, the Webpack ran fine. When I cloned the project onto my personal computer, it gave the following error:
ERROR in ./react_minesweeper.jsx
Module build failed: Error: Couldn't find preset "es2015" relative to directory "/Users/louisstephancruz/Desktop"
at /Users/louisstephancruz/Desktop/w6d5/minesweeper/node_modules/babel-core/lib/transformation/file/options/option-manager.js:298:19
at Array.map (native)
at OptionManager.resolvePresets (/Users/louisstephancruz/Desktop/w6d5/minesweeper/node_modules/babel-core/lib/transformation/file/options/option-manager.js:269:20)
Here's my webpack.config.js:
module.exports = {
entry: './react_minesweeper.jsx',
output: {
path: './',
filename: 'bundle.js',
},
module: {
loaders: [
{
test: [/\.jsx?$/, /\.js?$/],
exclude: /(node_modules)/,
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
}
]
},
devtool: 'source-map',
resolve: {
extensions: ['', '.js', '.jsx' ]
}
};
Here's my package.json:
{
"name": "minesweeper",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --inline"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"babel": "^6.5.2",
"babel-core": "^6.17.0",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.16.0",
"babel-preset-react": "^6.16.0",
"webpack": "^1.13.2",
"webpack-dev-server": "^1.16.2"
},
"dependencies": {
"react": "^15.3.2",
"react-dom": "^15.3.2"
}
}
My version of node is: v5.6.0
My version of npm is: 3.6.0
npm i or npm install
should install all the packages in your package.json dependencies and dev dependencies (so long as your NODE_ENV environment variable does not equal production).
To check if you have a particular package installed you may do:
npm ls babel-preset-es2015
If for some reason your NODE_ENV is production and you would like to install dev dependencies you can use:
npm install --only=dev
Conversely, the following may be used on production machines that are dealing with already built code and do not need access to any development dependencies:
npm install --only=prod
I'd recommend creating a .babelrc in the root of your repo with the following content:
{ "presets": [ "es2015", "react" ] }
For the webpack config you may want to specify some other options:
{ context: __dirname
, resolve: { root: __dirname, extensions: [ '.js', '.jsx', '.json' ] }
}
in addition to the rest of your configuration, this tells webpack where the root directory of the bundling should take place from and what file extensions to treat as modules (which extensions you can omit in require / import statements).
I'd recommend checking out webpack's resolve.extensions for more information on that bit.
npm install babel-preset-es2015
does that help?
I resolved this issue when I removed .babelrc (hidden) file from "/Users/username" directory.
For me, I had to install the package globally.
npm install babel-preset-es2015 --save -g
npm install babel-preset-es2015 babel-preset-stage-2 --save
It's worked for me.