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.
Related
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 need a bit of help with initial setup of webpack to serve my app with hot reloading. I've followed this guide to set up React+Webpack with Typescript, but I'd like to know how I can go about setting it up so I can call "npm start" and have it compile and host the app, with hot reloading.
My webpack.config.js looks like the following:
module.exports = {
mode: "production",
// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx"]
},
module: {
rules: [
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader"
}
]
},
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader"
}
]
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: {
"react": "React",
"react-dom": "ReactDOM"
}
};
And my package.json looks like this:
{
"name": "client",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "test"
},
"author": "DMW",
"license": "ISC",
"devDependencies": {
"#types/react": "^16.9.15",
"#types/react-dom": "^16.9.4",
"source-map-loader": "^0.2.4",
"ts-loader": "^6.2.1",
"typescript": "^3.7.3",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0"
},
"dependencies": {
"react": "^16.12.0",
"react-dom": "^16.12.0"
},
"keywords": []
}
I'd really appreciate it if someone could link me to something to get this working. I'm a bit intimidated by the search results I've found.
EDIT: I ended up using npx create-react-app app --template typescript instead, as detailed here: https://create-react-app.dev/docs/adding-typescript/
Welcome to the community.
Regarding the settings for webpack try adding this to your package.json
"scripts": {
"start": "webpack-dev-server --mode development --open --hot"
},
When you do npm start, start will be taken from this script and would run app as webpack. Also --hot will ensure that hot reload is true, i.e. reload on any changes that you do to the app automatically.
Be sure to change the mode to production when pushing the app to production so that you don't get all the errors and warnings in the production. Hope it helps !!
I have a carousel file in which I want to get index.js and build block.build.js, so my webpack.config.js is:
var config = {
entry: './index.js',
output: {
path: __dirname,
filename: 'block.build.js',
},
devServer: {
contentBase: './Carousel'
},
module : {
rules : [
{
test: /.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['react', 'es2015'],
plugins: ['transform-class-properties']
}
}
]
}
};
module.exports = config;
The package.json which I use is below:
{
"name": "carousel",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"dependencies": {
"#babel/core": "^7.0.0-beta.40",
"babel-cli": "^6.26.0",
"babel-loader": "^8.0.0-beta.0",
"babel-plugin-lodash": "^3.3.2",
"babel-plugin-react-transform": "^3.0.0",
"babel-preset-react": "^6.24.1",
"cross-env": "^5.1.3",
"lodash": "^4.17.5",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-swipeable": "^4.2.0",
"styled-components": "^3.2.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --watch",
"start": "webpack-dev-server --open",
"build": "webpack"
},
"devDependencies": {
"webpack": "^4.1.1",
"webpack-cli": "^2.0.10",
"webpack-dev-server": "^3.1.0"
},
"author": "brad traversy",
"license": "ISC"
}
… but I get this error message:
ERROR in ./index.js
Module build failed: Error: Plugin/Preset files are not allowed to export objects, only functions.
Does anyone know how to solve this?
You're using a combination of Babel 6 and Babel 7. There is no guarantee of compatibility across versions:
"#babel/core": "^7.0.0-beta.40",
"babel-cli": "^6.26.0",
"babel-loader": "^8.0.0-beta.0",
"babel-plugin-lodash": "^3.3.2",
"babel-plugin-react-transform": "^3.0.0",
"babel-preset-react": "^6.24.1",
should be
"#babel/core": "^7.0.0-beta.40",
"#babel/cli": "^7.0.0-beta.40",
"babel-loader": "^8.0.0-beta.0",
"babel-plugin-lodash": "^3.3.2",
"babel-plugin-react-transform": "^3.0.0",
"#babel/preset-react": "^7.0.0-beta.40",
and
query: {
presets: ['react', 'es2015'],
plugins: ['transform-class-properties']
}
would be
query: {
presets: ['#babel/react', '#babel/es2015'],
plugins: ['#babel/proposal-class-properties']
}
I'm also confused because you didn't mention #babel/proposal-class-properties in your package.json, but assuming it is in there it should also be updated.
It happened to me and a simple solution for me was to uninstall babel-loader#8^ and #babel/core:
npm uninstall --save babel-loader
npm uninstall --save #babel/core
… and then to install version 7 babel-loader:
npm install --save-dev babel-loader#^7
Got the same issue in my webpack/react project - it seems that there was an issue with the .babelrc file.
I updated it as seen below and it did the trick:
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
]
}
Also from babel-loader v8, they have changed a little bit:
webpack 4.x | babel-loader 8.x | babel 7.x
npm install -D babel-loader #babel/core #babel/preset-env webpack
webpack 4.x | babel-loader 7.x | babel 6.x
npm install -D babel-loader#7 babel-core babel-preset-env webpack
(same thing for #babel/preset-react instead of babel-preset-react).
There are upgrades in babel 7 from version 6 refer to https://babeljs.io/docs/en/v7-migration. To solve the current problem/error
Install
npm install --save-dev #babel/preset-react
npm install --save-dev #babel/preset-env
then in .babelrc the dependency for presets should look like
{
"presets":["#babel/preset-env", "#babel/preset-react"],
"plugins": [
"react-hot-loader/babel", "transform-object-rest-spread"]
}
this worked for me:
npm uninstall --save babel-loader
npm uninstall --save #babel/core
npm install --save-dev babel-loader#^7
and in babelrc:
"presets" : ["es2015", "react"]
This solution worked for me:
npm install babel-loader babel-preset-react
then in .babelrc
{
"presets": [
"#babel/preset-env", "#babel/preset-react"
]
}
then run npm run start, webpack will generate the dist directory.
I had "stage-1" within my presets in .babelrc, so I removed that and the error went away
in webpack.config.js file add
module: {
rules: [{
loader: 'babel-loader',
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
options: { presets: ['#babel/env','#babel/preset-react'] }
},
]
}
and create a file named .babelrc
{ "presets": [
"#babel/preset-env",
"#babel/preset-react"
]
}
and in package.json must install these dependencies and devDependencies
"dependencies": {
"#babel/preset-env": "^7.16.0",
"#babel/preset-react": "^7.16.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"validator": "^13.7.0",
"webpack": "^5.62.1"
},
"devDependencies": {
"#babel/core": "^7.16.0",
"#types/react": "^17.0.34",
"#types/react-dom": "^17.0.11",
"babel-loader": "^8.2.3",
"nodemon": "^2.0.14",
"webpack-cli": "^4.9.1"
}
first, remove installed packages
if yarn use
yarn remove babel-core babel-loader babel-preset-env babel-preset-react
if npm use
npm uninstall babel-core babel-loader babel-preset-env babel-preset-react
Then install packages
install using yarn
yarn add -D #babel/core babel-loader #babel/preset-env #babel/preset-react
install using npm
npm add -D #babel/core babel-loader #babel/preset-env #babel/preset-react
Replace your .babelrc file following code this fix my issue
{
"presets": ["module:metro-react-native-babel-preset"]
}
The solution that worked for me using Yarn was:
yarn add babel-loader babel-preset-react #babel/preset-env #babel/preset-react -D
then in .babelrc.json or only .babelrc
{
"presets": [
"#babel/preset-env", "#babel/preset-react"
]
}
then run webpack or yarn webpack, it'll generate the dist on the project root directory.
Clearly seems like you're facing issues due to the non-compatibility of babel-preset-react and babel-preset-env modules. These modules are now obsolete so you may need to install their latest alternatives. #babel/core seems to be fine.
Here's what you need to do:
Remove all the old babel-related modules. Although it's not necessary to remove all of them but I would suggest you start with a clean slate.
yarn remove babel-preset-react babel-preset-env
Or if you're using npm then enter the following code in the terminal:
npm uninstall babel - preset - react babel - preset - env
Once the old modules are removed, install the new ones. I'm showing codes for both yarn and npm
yarn add #babel/core #babel/preset-react #babel/preset/env --save
npm i #babel/core #babel/preset-react #babel/preset/env --save
Note that even if you already have #babel/core module installed, reinstall it as it helped me run my code for whatever reasons.
After that, as a final step, go ahead and edit your .babelrc file with this code:
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
]
}
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.
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)