Package issues with babel - reactjs

Community, just started with React and I've created the simplest stuff ever. Does it work? Of course... not.
I've been googling and tried to make those adjustments described. But still, my killer application won't run and I seem to end up with two different error messages depending on the actions i take:
"Babel Plugin/Preset files are not allowed to export objects, only functions"
"Uncaught ReferenceError: process is not defined"
I honestly don't know how to proceed, I've tried to upgrade to the latest "#babel/...", tried to downgrade to earlier babel versions, but React is playing hide and seek with me.
// app.js
import React from 'react';
import ReactDOM from 'react-dom';
const text = <p>Hello world</p>
ReactDOM.render(text, document.getElementById('app'));
// babel.rc
{
"presets": [
"env",
"react"
]
}
// webpack.config.js
const path = require('path');
module.exports = {
entry: './src/app.js',
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
mode: 'none',
module: {
rules: [{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
}]
}
};
// package.json
{
"name": "killer-app",
"version": "1.0.0",
"main": "index.js",
"author": "",
"license": "",
"scripts": {
"serve": "live-server public/",
"build": "webpack --watch",
"build-babel": "babel src/app.js --out-file=public/scripts/app.js --presets=env,react --watch"
},
"dependencies": {
"babel-cli": "^6.26.0",
"babel-core": "6.25.0",
"babel-loader": "7.1.1",
"babel-preset-env": "1.5.2",
"babel-preset-react": "6.24.1",
"live-server": "^1.2.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"validator": "^13.7.0",
"webpack": "^5.73.0"
},
"devDependencies": {
"webpack-cli": "^4.10.0"
}
}
As a side-note, the script "yarn run build-babel" doesn't work either. This script worked before I installed Webpack.
Any advice is appreciated! :)

Related

My app does not recognize JSX when I run babel

I am following instructions from a React course when setting up Babel with Webpack and now I get this error:
ERROR in ./src/app.js
Module build failed: SyntaxError: C:/Users/38164/Desktop/react-course-projects/indecision-app/src/app.js: Unexpected token (4:17)
image of the error
Here are the files from the setup:
app.js
import React from 'react';
import ReactDOM from 'react-dom';
const template = <p>testing</p>;
ReactDOM.render(template, document.getElementById('app'));
webpack.config.js
const path = require("path");
module.exports = {
entry: "./src/app.js",
output: {
path: path.join(__dirname, "public"),
filename: "bundle.js",
},
module: {
rules: [{
loader: "babel-loader",
test: /\.js$/,
exclude: /node_modules/
}
]
}
};
package.json
{
"name": "indecision-app",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"serve": "live-server public/",
"build": "webpack --watch",
"build-babel": "babel src/app.js --out-file=public/scripts/app.js --presets=env,react --watch"
},
"dependencies": {
"#babel/core": "^7.20.12",
"#babel/preset-env": "^7.20.2",
"#babel/preset-react": "^7.18.6",
"babel-cli": "6.24.1",
"babel-loader": "7.1.1",
"live-server": "^1.2.2",
"react": "16.0.0",
"react-dom": "16.0.0",
"validator": "8.0.0",
"webpack": "3.1.0"
}
}
.babelrs
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
]
}
Does anyone have any suggestions?
There is probably something minor, but I can't figure it out, as this is new for me.

How to install React on this project

I got this starting point app that was given to me, and I have to add react to it.
Its a basic node application with this sctructure:
MyProject
src
scripts
index.js
styles
index.scss
index.html
webpack
webpack.common.js
webpack.config.dev.js
webpack.config.prod.js
.babelrc
And I also got this package.json:
{
"name": "frontend-assessment-test",
"version": "1.0.0",
"description": "A frontend assessment test for our new pirates, which are willing to come on board.",
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config webpack/webpack.config.prod.js --colors",
"start": "webpack-dev-server --open --config webpack/webpack.config.dev.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/holidaypirates/frontend-assessment-test"
},
"author": "HolidayPirates",
"license": "MIT",
"bugs": {
"url": "https://github.com/holidaypirates/frontend-assessment-test/issues"
},
"devDependencies": {
"#babel/core": "^7.6.4",
"#babel/plugin-proposal-class-properties": "^7.5.5",
"#babel/plugin-syntax-dynamic-import": "^7.2.0",
"#babel/preset-env": "^7.6.3",
"babel-loader": "^8.0.6",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^5.0.4",
"cross-env": "^6.0.3",
"css-loader": "^3.2.0",
"eslint": "^6.5.1",
"eslint-loader": "^3.0.2",
"file-loader": "^4.2.0",
"html-webpack-plugin": "^4.0.0-beta.8",
"mini-css-extract-plugin": "^0.8.0",
"node-sass": "^4.13.0",
"sass-loader": "^8.0.0",
"style-loader": "^1.0.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.9",
"webpack-dev-server": "^3.9.0",
"webpack-merge": "^4.2.2"
},
"dependencies": {
"#babel/polyfill": "^7.6.0",
"core-js": "^3.3.3"
}
}
The babel file webpack.common.js has this configuration:
module.exports = {
entry: {
app: Path.resolve(__dirname, '../src/scripts/index.js')
},
output: {
path: Path.join(__dirname, '../build'),
filename: 'js/[name].js'
},
optimization: {
splitChunks: {
chunks: 'all',
name: false
}
},
plugins: [
new CleanWebpackPlugin(),
new CopyWebpackPlugin([
{ from: Path.resolve(__dirname, '../public'), to: 'public' }
]),
new HtmlWebpackPlugin({
template: Path.resolve(__dirname, '../src/index.html')
})
],
resolve: {
alias: {
'~': Path.resolve(__dirname, '../src')
}
},
module: {
rules: [
{
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto'
},
{
test: /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
use: {
loader: 'file-loader',
options: {
name: '[path][name].[ext]'
}
}
},
]
}
};
So because I saw threre is babel and style loader added to the webpack, if I would just install react and react-dom I could then start using react, if I would, in the index.js file import react and try to render a basic component.
But i get an error that Module build failed (from ./node_modules/babel-loader/lib/index.js):
Any idea what should I change in webpack config or which other package other than react
and react-dom in order to use react in index.js?
Thanks.
This code base will not support react js. Babel is used to converted the non understandable JS parts(mostly es6 or more) to es5. Simple babel configuration will not work in your case, every library has their own way of syntax.
For example, in order to use the react you should be installing babel-preset-react which is mandatory for react.
The above mentioned will understand the jsx and react functionalities and converts to es5.
Install react first, see the hello world is working then try to add your node.js project into that react, try to add small chunk continuously and see the result
My personal opinion if possible, keep UI(react) and backend(node) separate

webpack --watch does not want to process react component inside my index.js

I'm an old hat coder but I'm new to react, and it looks like I need another pair of eyeballs. I'm trying to setup a very basic React app from scratch (for practice) and it fires up fine, but when I try to run webpack --watch it's giving me the following error:
Here's the error I'm getting:
ERROR in ./src/index.js 5:16
Module parse failed: Unexpected token (5:16)
You may need an appropriate loader to handle this file type.
| import App from "./components/App.js";
|
> ReactDOM.render(<App />, document.getElementById('root'));
|
Here's my webpack.dev.config file:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/index.js",
output: {
path: path.join(__dirname, "/dist"),
filename: "index_bundle.js"
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
},
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: "./src/index.html"
})
]
};
Here's my package.json file:
{
"name": "react-boilerplate",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "webpack --watch --open",
"dev": "./node_modules/.bin/webpack-dev-server --mode development --config ./webpack.dev.config.js",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.3.3",
"#babel/preset-env": "^7.3.1",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^2.1.0",
"html-webpack-plugin": "^3.2.0",
"style-loader": "^0.23.1",
"webpack": "^4.29.5",
"webpack-cli": "^3.2.3",
"webpack-dev-server": "^3.2.0"
},
"dependencies": {
"react": "^16.8.3",
"react-dom": "^16.8.3"
}
}
Here's my .babelrc file:
{
"presets": ["#babel/preset-env", "#babel/preset-react"]
}
And finally... my index.js file
import React from "react";
import ReactDOM from "react-dom";
import App from "./components/App.js";
ReactDOM.render(<App />, document.getElementById('root'));
Directory Structure
This ought to be pretty straight-forward but I guess I'm just not seeing something. I'd appreciate any help!!

React Build Tool and Dev Server

Need some direction in terms of setting up the DEV environment and server for a project I'm starting for learning purposes. I want to use ReacJS with Bootstrap. Last time I worked with ReactJS, the build and server was already configured and I did not have to do much. We were using npm, gulp and bunch of other stuff.
Now that I'm trying to set this up, I'm not sure what to do. Can someone outline the simplest steps I can follow. I want to use latest versions of React eco-system and ideally have a build system to minify and combine files and stuff. There is so much info on Google that it is confusing. Another challenge I'm facing is that which versions to specify in package.json. Instead of gulp I decided to use webpack. Wasn't really sure if to go with gulp or webpack but decided to go with webpack. It is all working but not sure if I have the most updated versions of everything or need more stuff. I know for sure I don't have any watchers to auto refresh page on changes. For server I'm just using npm, not sure if that is all I need or there are any advantages of using others.
This is my package.json
{
"name": "track",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --port 3000 --progress --inline",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"html-webpack-plugin": "^2.29.0",
"path": "^0.12.7",
"react": "^16.0.0",
"react-dom": "^15.6.1",
"webpack": "^3.0.0",
"webpack-dev-server": "^2.5.0"
},
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.0",
"babel-preset-es2017": "^6.24.1",
"babel-preset-react": "^6.24.1"
}
}
Below is the webpack.config.js
module.exports = {
entry: './src/app.js',
output: {
path: __dirname,
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: { presets: [ 'es2015', 'react' ] }
}
]
}
};
Can someone outline the simplest steps I can follow.
I have faced very similar situation but i was working with react, redux, react-redux, other libraries and using axios for sending (http) request where i have to figure out by myself , Here's what i did :
NOTE: Make sure you have Node.js installed because i have Node along with webpack-dev-server here
npm init
Installed project dependencies using npm.
Inside script i have given link to node and webpack-dev-server as you can see
package.json
{
"name": "searchapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.3"
},
"dependencies": {
"babel-preset-stage-1": "^6.24.1",
"lodash": "^4.17.4",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-redux": "^5.0.6",
"react-router-dom": "^4.2.2",
"redux": "^3.7.2",
"redux-promise": "^0.5.3"
}
}
And this is webpack.config.js
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: {
bundle: './src/index.js',
},
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
loaders: [{
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-1']
}
}]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './'
}
};
And then make sure to include .babelrc
{
"presets": ["react", "es2015", "stage-1"]
}
And if you have github repository make sure to include .gitignore file so that these folders or files will not be included in git repo
.gitignore
/node_modules
bundle.js
npm-debug.log
.DS_Store
If you think all of this is overwhelming and too much for a start then best place to start with is create-react-app

Why do we need to use import 'babel-polyfill'; in react components?

I wonder if we user babel loader + all the presets, why do we need to include babel-polyfill anyway into our components? I just think that babel-loader should do all the job itself.
Examples were taken here https://github.com/reactjs/redux/tree/master/examples
What i am asking about is:
import 'babel-polyfill';
import React from 'react';
import { render } from 'react-dom';
import App from './containers/App';
Here is package example:
{
"name": "redux-shopping-cart-example",
"version": "0.0.0",
"description": "Redux shopping-cart example",
"scripts": {
"start": "node server.js",
"test": "cross-env NODE_ENV=test mocha --recursive --compilers js:babel-register",
"test:watch": "npm test -- --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/reactjs/redux.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/reactjs/redux/issues"
},
"homepage": "http://redux.js.org",
"dependencies": {
"babel-polyfill": "^6.3.14",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-redux": "^4.2.1",
"redux": "^3.2.1",
"redux-thunk": "^1.0.3"
},
"devDependencies": {
"babel-core": "^6.3.15",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.1.1",
"cross-env": "^1.0.7",
"enzyme": "^2.0.0",
"express": "^4.13.3",
"json-loader": "^0.5.3",
"react-addons-test-utils": "^0.14.7",
"redux-logger": "^2.0.1",
"mocha": "^2.2.5",
"node-libs-browser": "^0.5.2",
"webpack": "^1.9.11",
"webpack-dev-middleware": "^1.2.0",
"webpack-hot-middleware": "^2.9.1"
}
}
Here is webpack config example taken from https://github.com/reactjs/redux/tree/master/examples
var path = require('path')
var webpack = require('webpack')
module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: [
'webpack-hot-middleware/client',
'./index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin()
],
module: {
loaders: [
{
test: /\.js$/,
loaders: [ 'babel?presets[]=react,presets[]=es2015,presets[]=react-hmre' ],
exclude: /node_modules/,
include: __dirname
},
{
test: /\.json$/,
loaders: [ 'json' ],
exclude: /node_modules/,
include: __dirname
}
]
}
}
Babel transpiles your code to something that browsers can understand, but the resulting code uses features that may or may not work in every single browser. For example Object.assign is not supported in all browsers, so babel-polyfill fills the holes. It's just a collection of polyfills that you would usually include anyway to have support for legacy browsers.
Consider this code:
const foo = {
name: 'Homer'
};
const bar = Object.assign({}, foo, {age: '?'});
console.log(Object.keys(foo), Object.keys(bar));
Babel will transpile this to the almost identical:
'use strict';
var foo = {
name: 'Homer'
};
var bar = Object.assign({}, foo, { age: '?' });
console.log(Object.keys(foo), Object.keys(bar));
because this is normal old school JS syntax. However, that doesn't mean that the native functions used are implemented in all browsers, so we need to include the polyfill.

Resources