Babel doesn't recognise React component - reactjs

I'm trying to use babel to compile some React code into javascript. My project directory is setup like this:
In my App.jsx file under src I have:
const continents = ['Africa','America','Asia','Australia','Europe'];
const helloContinents = Array.from(continents, c => `Hello ${c}!`);
const message = helloContinents.join(' ');
const element = (
<div title="Outer div">
<h1>{message}</h1>
</div>
);
ReactDOM.render(element, document.getElementById('contents'));
I have added a babel.rc file under src and it contains
{
"presets": [
["#babel/preset-env", {
"targets": {
"ie": "11",
"edge": "15",
"safari": "10",
"firefox": "50",
"chrome": "49"
}
}],
"#babel/preset-react"
]
}
And my package.json file looks like:
{
"name": "pro-mern-stack-2",
"version": "1.0.0",
"description": "learning MERN",
"main": "index.js",
"scripts": {
"start": "node server/server.js",
"test": "echo \"Error: no test specified\" && exit 1",
"compile": "babel src --out-dir public"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"#babel/cli": "^7.2.3",
"#babel/core": "^7.2.2",
"#babel/preset-env": "^7.2.3",
"#babel/preset-react": "^7.9.4",
"babel-preset-react": "^6.24.1"
}
}
When I run npm run compile I get the following error:
> pro-mern-stack-2#1.0.0 compile /home/vagrant/pro-mern-stack-2
> babel src --out-dir public
SyntaxError: src/App.jsx: Unexpected token (6:2)
4 |
5 | const element = (
> 6 | <div title="Outer div">
| ^
7 | <h1>{message}</h1>
8 | </div>
9 | );
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! pro-mern-stack-2#1.0.0 compile: `babel src --out-dir public`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the pro-mern-stack-2#1.0.0 compile script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/vagrant/.npm/_logs/2020-05-23T13_50_03_224Z-debug.log
vagrant#scotchbox:~/pro-mern-stack-2$
App.js is produced under public however the React component is commented out. To the best of my knowledge I have included everything I need for this to work. Any help would be appreciated.

It's not called babel.rc, but .babelrc, also your .babelrc must be in the root directory of your project, not in in your src/ directory.

Related

when i try to run the project with library #google/model-viewer by npm start i get this error

this is my package.json
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#google/model-viewer": "^1.11.0",
"react": "^18.0.0"
}
}
`PS C:\Users\AA\Desktop\3Dpro\my-app> npm start
npm ERR! Missing script: "start"
npm ERR!
npm ERR! Did you mean one of these?
npm ERR! npm star # Mark your favorite packages
npm ERR! npm stars # View packages marked as favorites
npm ERR!
npm ERR! To see a list of scripts, run:
npm ERR! npm run
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\AA\AppData\Local\npm-cache_logs\2022-04-05T20_22_54_567Z-debug-0.log
PS C:\Users\AA\Desktop\3Dpro\my-app>
I am using a library for 3D ( #google/model-viewer)
this is my code:
import '#google/model-viewer'
import React, { Component } from 'react'
export default class PersonalDesign extends Component {
render() {
return (
)
}
}
Looks like your package.json file is missing the scripts for some reason. Add them back in. You also appear to be missing the react-dom and react-scripts dependencies.
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#google/model-viewer": "^1.11.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-scripts": "^4.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
After updating/saving your package.json file, run npm i to install the dependencies and then run npm start to start your project.

Module parsing failed while using webpack 4 for a react project

I am having trouble configuring my react project with Webpack 4.
I am printing my file contents here:
package.json (script part):
{
"name": "todo",
"version": "1.0.0",
"description": "a ToDo app built using react and webpack",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack --mode development ./src/index.js --output ./dist/main.js",
"build": "webpack --mode production ./src/index.js --output ./dist/main.js"
},
"author": "themaskedbit",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^8.0.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"webpack": "^4.26.1",
"webpack-cli": "^3.1.2"
},
"dependencies": {
"react": "^16.6.3",
"react-dom": "^16.6.3"
}
}
babelrc:
{
"presets": [
"#babel/preset-env", "#babel/preset-react"
]
}
webpack.config.js :
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
};
index.js :
import data from "./data/items";
import React from 'react';
import {render} from 'react-dom';
import ListItem from "./components/listItem";
render(
<ListItem items={data}/>,
document.getElementById("app")
);
With the above configuration, while I am trying to run the webpack using dev script. I am getting the below error:
$ npm run dev
> todo#1.0.0 dev C:\Users\rejir\Documents\HP works\ToDo
> webpack --mode development ./src/index.js --output ./dist/main.js
Hash: 0fac7774f9aa4bb8aeed
Version: webpack 4.26.1
Time: 234ms
Built at: 2018-11-28 19:50:35
Asset Size Chunks Chunk Names
main.js 4.01 KiB main [emitted] main
Entrypoint main = main.js
[./src/index.js] 227 bytes {main} [built] [failed] [1 error]
ERROR in ./src/index.js 9:4
Module parse failed: Unexpected token (9:4)
You may need an appropriate loader to handle this file type.
|
| render(
> <ListItem items={data}/>,
| document.getElementById("app")
| );
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! todo#1.0.0 dev: `webpack --mode development ./src/index.js --output ./dist/main.js`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the todo#1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\rejir\AppData\Roaming\npm-cache\_logs\2018-11-28T14_20_35_305Z-debug.log
Please help me with the webpack configuration.
My understanding is that I am including the babel-loader in a wrong way. Searching through stackoverflow answers didn't give me a clue to solve my problem. If any fellow developers can help me in solving this issue, it will be good for me.
Thanks in advance.
You need to pass the config file while running webpack command
"dev": "webpack --mode development ./src/index.js --output ./dist/main.js --config webpack.config.js",
"build": "webpack --mode production ./src/index.js --output ./dist/main.js --config webpack.config.js"
More info here - use-custom-configuration-file

I'm trying to deploy my Gatsby Blog on Github pages, but "npm run deploy" returns error

I tried to build and deploy my Gatsby blog on github pages.
I followed Gatsby's docs about deploy: https://www.gatsbyjs.org/docs/deploy-gatsby/
I run the following command:
npm run deploy
I encountered:
Cannot read property 'email' of null
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gatsby-starter-hello-world# deploy: `gatsby build && gh-pages -b master -d public`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the gatsby-starter-hello-world# deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/ChaewonKong/.npm/_logs/2018-09-20T09_34_57_114Z-debug.log
This is my gatsby-config.js looks like:
module.exports = {
siteMetadata: {
title: `Leon Kong's Blog`
},
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
name: `src`,
path: `${__dirname}/src/`
}
},
`gatsby-transformer-remark`
]
};
This is my package.json:
{
"name": "gatsby-starter-hello-world",
"description": "Gatsby hello world starter",
"license": "MIT",
"scripts": {
"develop": "gatsby develop",
"build": "gatsby build",
"serve": "gatsby serve",
"deploy": "gatsby build && gh-pages -b master -d public"
},
"dependencies": {
"gatsby": "^2.0.0",
"gatsby-source-filesystem": "^2.0.1",
"gatsby-transformer-remark": "^2.1.3",
"react": "^16.5.1",
"react-dom": "^16.5.1"
},
"devDependencies": {
"gh-pages": "^2.0.0"
}
}
Plus, I'm trying to make an user/organization site like:
https://username/github.io
I'm just guessing email is not set in your git config. Try to run git config user.email to see if it's there.
You can follow this article on how to add it https://help.github.com/articles/setting-your-commit-email-address-in-git/.
Also, here are docs on how to set a github page.

Getting an error while trying to run build

I am not able run my React Application using webpack. I am stuck with an error which I am unable to understand.
I am getting the following error on my terminal while trying to run the build:
The syntax of the command is incorrect.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! reactjs-basics#1.0.0 build: webpack -d && copy 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 reactjs-basics#1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Pratik\AppData\Roaming\npm-cache_logs\2018-05-06T07_21_12_207Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! reactjs-basics#1.0.0 start: npm run build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the reactjs-basics#1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Pratik\AppData\Roaming\npm-cache_logs\2018-05-06T07_21_12_256Z-debug.log
Package.json file is as below:
{
"name": "reactjs-basics",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "npm run build",
"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": [
"reactjs"
],
"author": "Pratik Basak",
"license": "ISC",
"devDependencies": {
"babel-loader": "^7.1.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"webpack": "^4.7.0",
"webpack-cli": "^2.1.2",
"webpack-dev-server": "^3.1.4"
}
}
webpack.config.js file is as follows:
const webpack = require('webpack');
const path = require('path');
const DIST_DIR = path.resolve(__dirname, 'dist');
const SRC_DIR = path.resolve(__dirname, 'src');
var config = {
entry: SRC_DIR + '/app/index.js',
output: {
path: DIST_DIR + '/app',
filename: 'bundle.js',
publicPath: '/app/'
},
module: {
rules: [
{
test: /\.js?/,
include: SRC_DIR,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-2']
}
}
]
}
};
module.exports = config;

missing script: start Electron

I try to create my first angular application using Electron, so i follow this guide here :
Electron Documentation
so i do step by step what is in the guide but when i do this command :
npm start
I get this error here:
$ npm start
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v6.9.4
npm ERR! npm v3.10.10
npm ERR! missing script: start
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! D:\FreeLancer\angular2electron\npm-debug.log
package.json
{
"name": "angular2electron",
"version": "1.0.1",
"description": "my first angularjs",
"main": "main.js",
"dependencies": {
"electron": "^1.4.13"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "LAIDANI Youcef",
"license": "MIT"
}
Someone have an idea about this problem?
Thank you.
npm start runs the script in the package.json. To get an electron app running with it just add electron . to the start script :
package.json
{
"name": "angular2electron",
"version": "1.0.1",
"description": "my first angularjs",
"main": "main.js",
"dependencies": {
"electron": "^1.4.13"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start" : "electron ."
},
"author": "LAIDANI Youcef",
"license": "MIT"
}

Resources