I am trying to use ES2017 in my React project with webpack
I have a .babelrc file, and a package.json.
this is my .babelrc file:
{
"presets": ["es2017"]
}
and this is package.json:
{
"name": "myapp",
"version": "1.0.0",
"private": true,
"devDependencies": {
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-plugin-syntax-async-functions": "^6.13.0",
"babel-plugin-syntax-trailing-function-commas": "^6.22.0",
"babel-plugin-transform-async-to-generator": "^6.22.0",
"babel-preset-es2017": "^6.22.0",
"enzyme": "^2.4.1",
"react-addons-test-utils": "^15.3.0",
"react-scripts": "^0.4.0",
"webpack": "^2.2.1"
},
"dependencies": {
"css-loader": "^0.26.1",
"react": "^15.3.0",
"react-bootstrap": "^0.30.7",
"react-dom": "^15.3.0",
"react-redux": "^4.4.5",
"react-router": "^3.0.0",
"react-router-redux": "^4.0.7",
"redux": "^3.5.2",
"redux-logger": "^2.6.1",
"redux-thunk": "^2.1.0",
"style-loader": "^0.13.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject",
"test": "react-scripts test"
},
"eslintConfig": {
"extends": "./node_modules/react-scripts/config/eslint.js"
}
}
When I am trying to use double colon, console reports syntax error
<div onMouseEnter={::this.mouseEnter()}>
</div>
anything wrong?
In order to make use of the :: operator to bind the function, you need to use the babel-plugin-transform-function-bind plugin.
Install it using the below command
npm install --save-dev babel-plugin-transform-function-bind
and then in your .babelrc include it as
{
"presets": ["react", "stage-0", "es2015"],
"plugins": ["transform-function-bind"]
}
Make sure you install the above using
npm install -S babel-preset-stage-0 babel-preset-2015 babel-preset-react
:: is the ES2015+ function bind syntax and not ES2017
Read more about it here :
Not an answer per se, but you can achieve similar behavior by declaring class methods as lambdas (or any other method mentioned here) :
export class MyComponent extends React.Component {
mouseEnter = () => {
console.log('hover');
}
render() {
return <h1 onMouseEnter={this.mouseEnter}>hi!</h1>
}
}
Related
This post relates to a rapidly changing event.
After running 'npm update' on my app, I am suddenly getting the following error:
Compiled with problems:
ERROR in ./node_modules/pako/lib/zlib/trees.js 257:106
Module parse failed: Unexpected token (257:106)
File was processed with these loaders:
* ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
| * not null.
| */
> function gen_bitlen(s, desc) /* deflate_state *s;*/ /* tree_desc *desc; /* the tree descriptor */*/{
| var tree = desc.dyn_tree;
| var max_code = desc.max_code;
Strange. So I compared it to the same app running on a different workstation with the same code, but where I did not run 'npm update'. The app works, no surprise.
I've seen other posts with this error, but their solutions do not seem to apply to my environment.
I cannot figure out why it's not working on my main workstation. If I copy over node_modules from the working station, the app starts fine. But as soon as I remove node_modules and package-lock.json and reinstall, the app will not start. I've removed node_modules/package-lock.json/clear npm cache. Doesn't help.
I'm comparing the module versions via 'npm ls', and they are identical on both workstations.
Both are running NodeJS 8.12.0 and npm 8.19.2
I looked at the file that it's erroring out on (pako/lib/zlib/trees.js), and it's identical on both systems.
I have no idea what 'pako' is, but using 'npm explain pako' it appears to have something to do with pdf-lib, which was never updated.
My app was built with create-react-app.
I'm at a complete loss. Here is my package.json:
{
"name": "foo",
"version": "0.1.0",
"description": "Foo",
"contributors": [],
"license": "UNLICENSED",
"private": true,
"dependencies": {
"#coreui/chartjs": "^2.0.0",
"#coreui/coreui-pro": "^3.4.2",
"#coreui/icons": "^2.1.0",
"#coreui/icons-pro": "^2.0.3",
"#coreui/icons-react": "^1.1.0",
"#coreui/react": "^3.4.6",
"#coreui/react-chartjs": "^1.1.0",
"#coreui/utils": "^1.3.1",
"#fortawesome/fontawesome-free-solid": "^5.0.13",
"#fortawesome/fontawesome-svg-core": "^6.1.1",
"#fortawesome/free-regular-svg-icons": "^6.1.1",
"#fortawesome/free-solid-svg-icons": "^6.1.1",
"#fortawesome/react-fontawesome": "^0.1.18",
"#nadavshaar/react-grid-table": "^1.0.0",
"#pdf-lib/fontkit": "^1.1.1",
"#react-firebase/auth": "^0.2.10",
"ag-grid-community": "^25.3.0",
"ag-grid-react": "^25.3.0",
"arithmetic": "^1.0.1",
"bootstrap": "^5.2.0",
"classnames": "^2.3.1",
"codemirror": "^5.63.3",
"core-js": "^3.19.1",
"downloadjs": "^1.4.7",
"firebase": "^9.12.1",
"firebase-admin": "^11.0.1",
"firebaseui": "^6.0.1",
"formik": "^2.2.9",
"mobx": "^6.3.3",
"mobx-react": "^7.1.0",
"pdf-lib": "^1.17.1",
"prop-types": "^15.7.2",
"random-id": "^1.0.4",
"react": "^17.0.2",
"react-app-polyfill": "^2.0.0",
"react-awesome-button": "^6.5.1",
"react-big-calendar": "^0.33.6",
"react-bootstrap": "^2.4.0",
"react-collapsible": "^2.10.0",
"react-cookie-consent": "^8.0.1",
"react-dom": "^17.0.2",
"react-firebase-hooks": "^5.0.3",
"react-firebaseui": "^6.0.0",
"react-grid-layout": "^1.3.0",
"react-range": "^1.8.12",
"react-redux": "7.2.4",
"react-router-dom": "^5.3.0",
"react-select": "^4.3.1",
"react-text-mask-hoc": "^0.11.0",
"react-toastify": "^9.0.8",
"reactstrap": "^8.10.0",
"redux": "4.1.0",
"rpg-dice-roller": "1.6.0",
"sass": "^1.55.0",
"sillyname": "^0.1.0",
"spinkit": "2.0.1",
"string-math": "^1.2.2",
"styled-components": "^5.3.3",
"yup": "^0.32.11"
},
"devDependencies": {
"mutationobserver-shim": "^0.3.7",
"react-scripts": "^5.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test:cov": "npm test -- --coverage --watchAll=false",
"test:debug": "react-scripts --inspect-brk test --runInBand",
"eject": "react-scripts eject",
"zip": "git archive -o coreui-pro-react-admin-template-v$npm_package_version.zip -9 HEAD"
},
"bugs": {
"url": "https://github.com/coreui/coreui-free-react-admin-template/issues"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 10",
"not op_mini all"
],
"jest": {
"collectCoverageFrom": [
"src/**/*.{js,jsx}",
"!**/*index.js",
"!src/serviceWorker.js",
"!src/polyfill.js"
]
},
"engines": {
"node": ">=8.16",
"npm": ">=6"
}
}
I made a workaround. I compared my old and new package-lock.json of my projects and I saw some #babel lib version changed to v7.20.2 (on Nov 4th, 2022). I overwrote the new package-lock.json with old one and my react app built and ran successfully again.
I then changed (downgrade) some of #babel package in my package.json and it is solved the problem:
Put this lines into your package.json file:
"#babel/core": "7.19.6",
"#babel/generator": "7.19.6",
"#babel/compat-data": "7.19.4",
"#babel/helper-compilation-targets": "7.19.3",
"#babel/helper-create-class-features-plugin":"7.19.0",
"#babel/helper-module-transforms":"7.19.6",
https://github.com/babel/babel/issues/15132
Babel fixed their issue. you can use the latest version and it should work now. (I verified it)
extending #Attila Szombathelyi's answer,
If you are using CRA, you can make use of resolutions in your package.json
{
"name": "app-name",
"version": "1.0.0",
"dependencies": {
...
},
...
"resolutions": {
"#babel/core": "7.19.6",
"#babel/generator": "7.19.6",
"#babel/compat-data": "7.19.4",
"#babel/helper-compilation-targets": "7.19.3",
"#babel/helper-create-class-features-plugin": "7.19.0",
"#babel/helper-module-transforms": "7.19.6",
"babel-loader": "8.2.5"
}
}
Solution in my case was to disable babel dependencies transpilation (which was enabled in our project).
How I did it? In my project vue-cli is used. So I have disabed transpileDependecies option in vue.config.js
If you are using a bare webpack you may also find appropriate solution for it or any other build system.
Workaround: As of npm 8.3.0, you can also use npm's
overrides in package.json. Delete package-lock.json and node_modules folder (recommended).
"overrides": {
"#babel/core": "7.19.6",
"#babel/generator": "7.19.6",
"#babel/compat-data": "7.19.4",
"#babel/helper-compilation-targets": "7.19.3",
"#babel/helper-create-class-features-plugin": "7.19.0",
"#babel/helper-module-transforms": "7.19.6"
}
I am getting uncaught reference error in react native why so ? I am able to build successfully but the bundler is showing this error. How can I fix it ?
Note: I am using windows 10
Screenshot:
package.json:
{
"name": "rchampz",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"lodash": "^4.17.11",
"moment": "^2.22.2",
"react": "16.5.0",
"react-native": "0.57.0",
"react-native-dash": "^0.0.9",
"react-native-dropdownalert": "^3.5.0",
"react-native-material-dropdown": "^0.11.1",
"react-native-modal-datetime-picker": "^6.0.0",
"react-native-paper": "^2.0.1",
"react-native-render-html": "^3.10.0",
"react-native-timeline-theme": "^0.0.9",
"react-native-vector-icons": "^5.0.0",
"react-navigation": "^2.14.2",
"react-redux": "^5.0.7",
"redux": "^4.0.0"
},
"devDependencies": {
"babel-jest": "23.6.0",
"jest": "23.6.0",
"react-test-renderer": "16.5.0"
},
"jest": {
"preset": "react-native"
}
}
.babelrc :
{
"presets": ["module:react-native"]
}
Either include preset of react-native in .bablerc or export presets from babel.config.js like below
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
'#babel/plugin-transform-runtime'
],
}
When in doubt, use react-native-cli to create a new ReactNative project, and see the build files. I just did that, and here's what I have :
package.json
{
"name": "someproject",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.5.0",
"react-native": "0.57.1"
},
"devDependencies": {
"babel-jest": "23.6.0",
"jest": "23.6.0",
"metro-react-native-babel-preset": "0.47.1",
"react-test-renderer": "16.5.0"
},
"jest": {
"preset": "react-native"
}
}
.babelrc :
{
"presets": ["module:metro-react-native-babel-preset"]
}
Note that if you changed your package.json, some cleaning might be needed first
I want to use react-native-aes-crypto but it need react-native. I install it but it continue to say that it can't find it.
I have try to reinstall it, add his dependencies but nothing work.
My package.json:
{
"name": "neko_web",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome": "^1.1.8",
"#fortawesome/react-fontawesome": "0.0.20",
"#trendmicro/react-sidenav": "^0.4.5",
"ajv": "^6.5.1",
"create-react-app": "^1.5.2",
"font-awesome": "^4.7.0",
"moment": "^2.22.2",
"react": "16.3.1",
"react-cookie": "^2.2.0",
"react-dom": "^16.4.0",
"react-icons-kit": "^1.1.5",
"react-moment": "^0.7.7",
"react-native": "^0.55.4",
"react-native-aes-crypto": "^1.2.1",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4",
"react-sidenav": "^2.1.3"
},
"scripts": {
"start": "PORT=3005 react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
The error is getting when I do import Aes from 'react-native-aes-crypto'
UPDATE 1:
I have tried to change version of react-native to 0.55.0, no more audit fix to do but it display the same error.
Install the package react-native-cli for fix that.
If you want an another package who doesn't require react-native, use simple-encryptor.
We have a small hackaton in our office and I have some problem with react-native-maps. I am getting error: undefined is not an object (evaluating 'PropTypes.string') which is going to MapMaker.js file from MapView.
There is my package.json file:
{
"name": "niehandlowa",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"rnpm": {
"assets": [
"./assets/fonts/"
]
},
"dependencies": {
"native-base": "^2.3.10",
"prop-types": "^15.6.1",
"react": "16.3.0-alpha.3",
"react-native": "^0.54.2",
"react-native-elements": "^0.19.0",
"react-native-maps": "^0.4.2",
"react-native-router-flux": "^4.0.0-beta.30",
"react-redux": "^5.0.7",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"babel-jest": "22.4.3",
"babel-preset-react-native": "4.0.0",
"jest": "22.4.3",
"react-test-renderer": "16.3.0-alpha.3"
},
"jest": {
"preset": "react-native"
}
}
I will be glad because today our team want to finish great app that we started yesterday :)
Have a nice day everybody!
It is most likely related to the version of react-native-maps you are using. v0.4.2 is using PropTypes from react and the library separated now. You should use a more resent version of the react-native-maps library
I am using the Airbnb lint settings for my react app
and the line
import React from 'react';
Gives the error
Missing file extension for "react" (import/extensions)
Any idea how to fix this?
{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.17.1",
"firebase": "^4.6.2",
"jquery": "^3.2.1",
"react": "^16.1.1",
"react-dom": "^16.1.1",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-scripts": "^1.0.17"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"sass": "sass --watch src/styles/scss:src/styles/css",
"sass:build": "sass src/styles/scss:src/styles/css"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-react": "^7.5.1"
}
}
In the rules object of your .eslintrc set "import/extensions" to never or 0 to avoid this.
You should check if you have eslint-plugin-import installed, Airbnb style guide configuration uses it to handle linting of imports and exports. If you don't have it, it results in the error Unable to resolve path to module '_module-name_' and/or Missing file extension for "_module-name_" (import/extensions).
Also since you have it installed in dev-dependencies, you can run something like ./node_modules/.bin/eslint . or setup eslint globally to just run eslint .