Adding react-lds import in react component resulting in failed compilation - reactjs

Please refer import that is causing error.It compiles without any issue if I comment this line.
import React, {Component} from 'react';
import {returnCurrencies} from '../reducers/reducer_currency';
import {DropdownMenu, DropdownMenuList, DropdownMenuListItem} from 'react-lds';
export default class CurrencyDropDown extends Component
{
constructor(props)
{
super(props);
//console.log("props",props);
}
renderCurrencies(curr)
{
//console.log("Curr ", curr);
return (<option key={curr} value={curr}>{curr}</option>);
}
render()
{
if(!this.props.currencies)
{
return (<div></div>);
}
let currencyList = returnCurrencies(this.props.currencies);
return (<div>
<select value={this.props.initValue} onChange=
{this.props.selectCurrency}>
{currencyList.map(this.renderCurrencies)}
</select>
</div>);
}
}
Package.json
{
"name": "redux-simple-starter",
"version": "1.0.0",
"description": "Simple starter package for Redux with React and Babel support",
"main": "index.js",
"repository": "",
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
"test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
"test:watch": "npm run test -- --watch"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"chai": "^3.5.0",
"chai-jquery": "^2.0.0",
"jquery": "^2.2.1",
"jsdom": "^8.1.0",
"mocha": "^2.4.5",
"react-addons-test-utils": "^0.14.7",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"axios": "^0.16.2",
"babel-preset-stage-1": "^6.1.18",
"lodash": "^3.10.1",
"prop-types": "^15.5.10",
"react": "^0.14.9",
"react-dom": "^0.14.9",
"react-lds": "^1.1.11",
"react-redux": "4.3.0",
"react-router": "^2.0.1",
"redux": "^3.0.4",
"redux-promise": "^0.5.3"
}
}
Failed to compile with below error.
ERROR in ./~/moment-timezone/data/packed/latest.json
Module parse failed: C:..\XXXX\node_modules\moment-timezone\data\packed\latest.json Unexpected token (2:10)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:10)
at Parser.pp$4.raise (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:2221:15)
at Parser.pp.unexpected (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:603:10)
at Parser.pp.semicolon (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:581:61)
at Parser.pp$1.parseExpressionStatement (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:966:10)
at Parser.pp$1.parseStatement (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:730:24)
at Parser.pp$1.parseBlock (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:981:25)
at Parser.pp$1.parseStatement (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:709:33)
at Parser.pp$1.parseTopLevel (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:638:25)
at Parser.parse (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:516:17)
at Object.parse (C:..\XXXX\node_modules\webpack\node_modules\acorn\dist\acorn.js:3098:39)
# ./~/moment-timezone/index.js 2:15-51

It fails because it can not load the JSON file. You need to configure webpack to load the JSON files:
Install the json-loader:
npm install --save-dev json-loader
And adjust your webpack config loaders section to use something like this:
rules: [
...
{
test: /\.json$/,
use: 'json-loader'
}
...
]
Given that you are using the webpack v1 config may look a bit different to you.

Related

How do I create a react / react-dom webpack alias in next.js?

I have created an alias for react and react-dom in my next.config.js. This is my next.config.js:
var path = require("path");
module.exports = (phase) => {
return {
// typescript: {
// ignoreBuildErrors: false,
// },
webpack5: true,
webpack(config, options) {
// const { isServer } = options;
// if (!isServer) {
// config.resolve.fallback.fs = false;
// }
config.module.rules.push({
test: /\.svg$/,
loader: "#svgr/webpack",
});
config.resolve.alias["react"] = path.resolve(
__dirname,
"shared-js/node_modules/react"
);
config.resolve.alias["react-dom"] = path.resolve(
__dirname,
"shared-js/node_modules/react-dom"
);
console.log(path.resolve(__dirname, "shared-js/node_modules/react-dom"));
return config;
},
};
};
Why am I doing this?
I have a component library, which is based on material ui. This is imported to my nextjs app via a submodule (shared-js). This is the package.json for my component library:
{
"name": "my-component-library",
"version": "1.0.0",
"description": "",
"author": "",
"private": true,
"main": "dist/index.js",
"module": "dist/index.modern.js",
"umd": "dist/index.min.js",
"source": "src/index.js",
"scripts": {
"start": "rollup -c -w",
"build": "NODE_ENV=production rollup -c",
"build-dev": "NODE_ENV=develop rollup -c --minifyInternalExports=false",
"prettier": "prettier --write ./src"
},
"peerDependencies": {
"eslint": "^8.15.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"license": "ISC",
"dependencies": {
"#emotion/react": "^11.7.1",
"#emotion/styled": "^11.6.0",
"#mui/icons-material": "^5.4.2",
"#mui/material": "^5.4.1",
"#mui/styles": "^5.4.2",
"#mui/x-data-grid": "^5.7.0",
"#rollup/plugin-node-resolve": "^13.3.0",
"prop-types": "^15.8.1",
"react-beautiful-dnd": "^13.1.0",
"react-grid-layout": "^1.3.4",
"react-resizable": "^3.0.4",
"recharts": "^2.1.9",
"rollup-plugin-import-css": "^3.0.3",
"styled-components": "^5.3.3",
"use-react-screenshot": "^3.0.0"
},
"devDependencies": {
"#babel/cli": "^7.17.10",
"#babel/core": "^7.17.10",
"#babel/plugin-transform-runtime": "^7.17.10",
"#babel/preset-env": "^7.17.10",
"#babel/preset-react": "^7.16.7",
"#rollup/plugin-babel": "^5.3.1",
"#rollup/plugin-commonjs": "^21.0.2",
"#svgr/rollup": "^6.2.1",
"babel-eslint": "^10.1.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "^7.29.4",
"eslint-plugin-standard": "^5.0.0",
"prettier": "^2.6.2",
"rollup": "^2.72.1",
"rollup-plugin-delete": "^2.0.0",
"rollup-plugin-filesize": "^9.1.2",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-visualizer": "^5.6.0"
},
"files": [
"dist"
]
}
Here is my package.json for my nextjs app:
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"name": "frontend",
"version": "1.0.0",
"main": "index.js",
"repository": "",
"author": "",
"license": "MIT",
"dependencies": {
"next": "^12.1.5",
"react": "^18.1.0",
"react-dom": "^18.1.0"
}
}
And my folder structure for the project:
-.next
-node_modules
-pages
-shared-js
--node_modules
--package.json
package.json
next.config.js
The problem is, that I have two conflicting versions of react and react-dom (I guess) in my shared-js/node_modules and the other ones in the node_modules folder of my nextjs app. This caused the error "Invalid hook call" whenever I tried to import a component to my nextjs app. I looked into the documentation and saw that this might be because of two conflicting react / react-dom versions, which is indeed the case. So I tried to avoid this by defining this alias in my next.config.js.
Which led me here. Whenever I run npm run dev or npm run build for my nextjs app, I get this error, telling me that react-dom is apparently missing:
wait - compiling /_error (client and server)...
wait - compiling...
error - ./node_modules/next/dist/client/index.js:513:35
Module not found: Can't resolve 'react-dom/client'
Why is that? The path is correct, the dependency is there but still I get this error. Is there anything I am missing? Or something I am doing wrong?
What I already tried: Deleting node_mdoules and package-lock.json from both nextjs and my component library and reinstall them. Deleting .next and restart via npm run dev
that's how I solved the problem with the conflicting versions.
Good description of the problem: https://blog.maximeheckel.com/posts/duplicate-dependencies-npm-link/
My next.config.js
var path = require("path");
module.exports = {
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
config.resolve.alias["react"] = path.resolve("./node_modules/react");
return config;
},
};

Cannot find module '#babel/preset-register'

I am new to React and Jest and during my first experiment I got stuck with the following error when running jest --watchAll:
FAIL src/components/App.test.js
● Test suite failed to run
Cannot find module '#babel/preset-register' from '/Users/joaomatos/projects/dummy'
- If you want to resolve "#babel/register", use "module:#babel/register"
at Function.module.exports [as sync] (node_modules/resolve/lib/sync.js:74:15)
at resolveStandardizedName (node_modules/#babel/core/lib/config/files/plugins.js:101:31)
at resolvePreset (node_modules/#babel/core/lib/config/files/plugins.js:58:10)
at loadPreset (node_modules/#babel/core/lib/config/files/plugins.js:77:20)
at createDescriptor (node_modules/#babel/core/lib/config/config-descriptors.js:154:9)
at node_modules/#babel/core/lib/config/config-descriptors.js:109:50
at Array.map (<anonymous>)
at createDescriptors (node_modules/#babel/core/lib/config/config-descriptors.js:109:29)
at createPresetDescriptors (node_modules/#babel/core/lib/config/config-descriptors.js:101:10)
at presets (node_modules/#babel/core/lib/config/config-descriptors.js:47:19)
I installed several babel packages namely babel register, but I cannot find anything else related to #babel/preset-register. My package.json:
{
"name": "dummy",
"version": "0.1.0",
"private": true,
"dependencies": {
"#babel/preset-env": "^7.7.1",
"acorn": "^7.1.0",
"babel-plugin-transform-export-extensions": "^6.22.0",
"babel-preset-env": "^1.7.0",
"history": "^4.10.1",
"npm-preset": "^1.5.0",
"parcel": "^1.12.4",
"react": "^16.11.0",
"react-app-polyfill": "^1.0.4",
"react-dev-utils": "^9.1.0",
"react-dom": "^16.6.3",
"react-dropzone": "^10.1.10",
"react-file-drop": "^0.2.8",
"react-flip-move": "^3.0.4",
"react-redux": "^6.0.0",
"react-router-dom": "^4.3.1",
"react-scripts": "3.2.0",
"react-switch": "^5.0.1",
"react-test-renderer": "^16.11.0",
"redux": "^4.0.4",
"typescript": "^3.7.2"
},
"scripts": {
"dev": "parcel src/index.html",
"build": "parcel build src/index.html",
"start": "npm run build && live-server dist",
"test": "jest --watchAll"
},
"devDependencies": {
"#babel/core": "^7.7.2",
"#babel/plugin-proposal-class-properties": "^7.7.0",
"#babel/preset-react": "^7.7.0",
"#babel/preset-stage-2": "^7.0.0",
"#babel/register": "^7.7.0",
"babel-loader": "^8.0.6",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1"
}
}
and .babel.rc:
{
"plugins": [
"#babel/plugin-proposal-class-properties"
],
"env": {
"test": {
"presets": [
"env",
"#babel/preset-env",
"#babel/preset-react",
"#babel/preset-stage-2",
"#babel/register",
"stage-2",
"react"
],
"plugins": [
"transform-export-extensions",
"transform-class-properties"
],
"only": [
"./**/*.js",
"node_modules/jest-runtime"
]
}
}
And my test:
import React from 'react';
import Enzyme, {shallow} from 'enzyme';
import EnzymeAdapter from 'enzyme-adapter-react-16';
import App from './App';
Enzyme.configure({adapter: new EnzymeAdapter()});
test(
'renders without crashing',
() => {
const wrapper = shallow(<App />);
expect(wrapper).toBeTruthy();
}
);
Does anyone know why I am getting this error?
Thank you for your help.
Babel-register is not a preset, hence the error message you are facing. Are you sure you need it for a starter project? Try removing it from the presets array in babel.rc

SyntaxError: Unexpected token import at ScriptTransformer._transformAndBuildScript [duplicate]

I'm trying to learn how to do unit testing with React and jest. I've run into the following error:
Unexpected token import
Here are my babel presets:
{
"presets": ["es2015", "stage-0", "react"],
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
Here is the file i'm trying to test:
import jest from 'jest';
import React from 'react';
import { shallow } from 'enzyme';
import Step from '../src/components/step.js';
const wrapper = shallow(<Step />);
wrapper.find('a').simulate('click', { preventDefault() {} });
and here is my package.json:
{
"name": "react-simple-boilerplate",
"version": "1.0.0",
"description": "Simple and lightweight Boilerplate for ReactJS projects",
"scripts": {
"lint": "eslint src",
"start": "node server.js",
"test": "jest"
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^0.28.0",
"enzyme": "^3.2.0",
"enzyme-adapter-react-16": "^1.1.0",
"eslint": "^3.19.0",
"eslint-plugin-react": "^6.10.3",
"jest": "^21.2.1",
"node-sass": "^4.5.2",
"react-addons-test-utils": "^15.6.2",
"sass-loader": "^6.0.3",
"style-loader": "^0.16.1",
"webpack": "^2.4.1",
"webpack-dev-server": "^2.4.4"
},
"dependencies": {
"babel-loader": "^7.0.0",
"react": "^15.5.4",
"react-dom": "^15.5.4"
},
"main": "server.js"
}
Have no idea why its giving me this message?
It looks like babel-jest is missing among your dependencies. That's why jest is not running babel on your ES6+ code before executing tests.
I'm pretty sure you need to add your babel presets to test as well, jest sets the env variable to test.
So...
"test": {
"plugins": ["transform-es2015-modules-commonjs"],
"presets": ["es2015", "react", "stage-0"]
}

Error after updating react: TypeError: Cannot call a class constructor without |new|

Node version: v10.15.3
I'm working on a project. Everything worked fine, but after someone did npm install the following error is shown:
TypeError: Cannot call a class constructor without |new|
in SContainer.js
followed by:
The above error occurred in the <FluxContainer(SContainer)> component:
in FluxContainer(SContainer)
Consider adding an error boundary to your tree to customize error handling behavior.
I want to render a container with props, but apparently it doesn't work anymore. The way it was done is as follows:
search.js:
'use strict'
import React from 'react';
import ReactDOM from 'react-dom';
import SContainer from './containers/sContainer'
ReactDOM.render(
<SContainer page="search"/>,
document.getElementById('search')
);
and SContainer.js:
'use strict';
// import area
// framework related imports
import React from 'react';
import { Container } from 'flux/utils';
import {MuiThemeProvider} from '#material-ui/core/styles';
// application related imports
import ShopStore from './../../stores/shopStore';
import ContentContainer from './contentContainer';
import ProviderRegistration from '../../components/views/providerRegistration';
import ProviderServiceRegistration from '../../components/views/providerServiceRegistration';
// variables area
class SContainer extends React.Component {
constructor(props) {
super(props);
this.className = this.constructor.name;
this.state = {};
this.style = {
routerContent: {
flexGrow: 1
}
};
}
static getStores() {
let stores = [
ShopStore
];
return stores;
}
static calculateState(prevState) {
let state = {
shopStore : ShopStore.getState(),
}
return state;
}
render() {
const shopStore = this.state.shopStore;
const theme = shopStore.get('theme');
const renderPageObject = {
// /provider-page
"providerPage": <ProviderRegistration {...this.state} />,
// /search-page
"searchShop": <ContentContainer {...this.state} />,
// /service-registration
"serviceRegistration": <ProviderServiceRegistration {...this.state} />,
}
return (
<MuiThemeProvider theme={theme.object} >
<section className="container">
<div className='row app'>
<div className="col-sm-10">
<div id='routerContent' style={this.style.routerContent}>
{renderPageObject[this.props.page]}
</div>
</div>
</div>
</section>
</MuiThemeProvider>
);
}
}
export default Container.create(SContainer);
I found a workaround on github, which adviced to do something like this in the SContainer.js:
var fluxContainerConverter = require('./FluxContainerConverter');
export default Container.create(fluxContainerConverter.convert(ShopContainer));
and in FLuxContainerConverter.js:
module.exports = {
convert: function(containerClass) {
const tmp = containerClass;
containerClass = function(...args) {
return new tmp(...args);
};
containerClass.prototype = tmp.prototype;
containerClass.getStores = tmp.getStores;
containerClass.calculateState = tmp.calculateState;
return containerClass;
}
};
But this doesn't work either, I just get other errors, which ultimately lead to a Warning: Unexpected Fiber popped error. I'm pretty clueless about the error honestly so any help would be appreciated. If I forgot to post something important, let me know.
Here is the package.json:
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:dev": "webpack --config ./webpack.config.js --watch --progress --color",
"build:prod": "rm -rf build node_modules && npm i --unsafe-perm && webpack --config ./webpack-production.config.js --progress",
"yarn:prod": "rm -rf build node_modules && yarn install --unsafe-perm && webpack --config ./webpack-production.config.js --progress",
"build:preprod": "rm -rf build node_modules && npm i --unsafe-perm && webpack --config ./webpack.config.js --progress",
"yarn:preprod": "rm -rf build node_modules && yarn install && webpack --config ./webpack.config.js --progress",
"clean": "rimraf dist"
},
"author": "",
"license": "GPL",
"dependencies": {
"#material-ui/core": "^3.1.0",
"alt": "^0.18.4",
"axios": "^0.12.0",
"bcv-react-select": "^1.1.1",
"classnames": "^2.2.5",
"country-data": "0.0.29",
"dateformat": "^3.0.2",
"flux": "^3.1.3",
"history": "^4.7.2",
"immutable": "^3.8.2",
"jquery": "^3.3.1",
"material-design-icons": "^3.0.1",
"moment": "^2.14.1",
"online-event-tool": "1.0.2",
"react": "^16.8.6",
"react-addons-shallow-compare": "^15.2.1",
"react-addons-test-utils": "^15.6.2",
"react-avatar": "^3.4.3",
"react-copy": "^0.2.1",
"react-custom-scrollbars": "^4.2.1",
"react-dom": "^16.8.6",
"react-image-gallery": "^0.7.15",
"react-input-autosize": "^1.0.0",
"react-joyride": "^1.11.4",
"react-places-autocomplete": "^3.0.0",
"react-router-dom": "^4.2.2",
"react-select": "^1.0.0-beta14",
"react-slick": "^0.14.8",
"react-tabs": "^0.5.5",
"react-tap-event-plugin": "^1.0.0",
"sockjs-client": "^1.1.1",
"strip-loader": "^0.1.2",
"superagent": "^4.0.0-beta.5",
"unique-id-mixin": "^1.0.0",
"velocity-animate": "^1.4.2",
"velocity-react": "^1.2.1",
"vis": "^4.21.0",
"webpack-merge": "^4.1.0",
"xhr": "^2.4.1"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.9.1",
"babel-loader": "^6.2.4",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-3": "^6.24.1",
"css-loader": "^0.26.1",
"enzyme": "^3.2.0",
"enzyme-adapter-react-15": "^1.0.5",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.5",
"node-sass": "^4.5.0",
"react-addons-css-transition-group": "^15.5.2",
"react-controllables": "^0.6.0",
"react-pure-render": "^1.0.2",
"react-test-renderer": "^15.6.2",
"sass-loader": "^4.1.1",
"style-loader": "^0.13.1",
"transfer-webpack-plugin": "^0.1.4",
"uglifyjs-webpack-plugin": "^2.0.1",
"webpack": "^3.11.0",
"webpack-cli": "^3.1.0"
}
}
All right, after some while I figured it out.
I basically had to update all react libraries and other stuff, because some of the naming conventions changed.

SyntaxError: Unexpected reserved word on running mocha with enzyme

On running the enzyme test by using mocha. I am getting the error
(function (exports, require, module, __filename, __dirname) { import React fro
^^^^^^
SyntaxError: Unexpected reserved word
I have the below-given test script in my test file
import React from 'react';
import { assert } from 'chai';
import { render } from 'enzyme';
import Book from '../src/Book';
describe("Book", () => {
it("render text", () => {
const wrapper = render(<Book author="Dan Abramov" title="Redux and ReactJS" />);
assert.equal(wrapper.text(), 'Redux and ReactJS by Dan Abramov');
});
});
I have tried to replace import with require, did not work. It would be wonderful if someone give a helping hand to figure out the issue.
EDIT
I am including the package.json file below,
{
"name": "test-client",
"version": "0.0.1",
"description": "test companies client.",
"repository": "https://github.com/Test/test-client",
"main": "js/app.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha"
},
"dependencies": {
"classnames": "2.2.*",
"clone": "^1.0.2",
"es5-shim": "4.1.*",
"es6-object-assign": "1.0.*",
"es6-promise": "3.0.*",
"flux": "^2.1.1",
"font-awesome": "^4.5.0",
"form-data": "^1.0.0-rc3",
"hashids": "^1.0.2",
"howler": "^1.1.29",
"isomorphic-fetch": "^2.2.1",
"js-cookie": "^2.1.0",
"json3": "3.3.*",
"keymirror": "0.1.*",
"normalize.css": "^4.0.0",
"react": "^15.0.1",
"react-addons-create-fragment": "^15.0.1",
"react-dnd": "^2.1.4",
"react-dnd-html5-backend": "^2.1.2",
"react-dom": "^15.0.1",
"react-infinite": "^0.9.2",
"react-paginate": "^1.0.4",
"react-tooltip": "^2.0.0",
"socket.io-client": "1.3.*",
"twemoji": "^2.0.5",
"unorm": "^1.4.1"
},
"devDependencies": {
"browserify": "^6.2.0",
"catw": "^1.0.1",
"dockerode": "^2.0.4",
"dotenv": "^2.0.0",
"envify": "^3.4.0",
"gulp-awspublish": "^3.0.2",
"less": "^2.6.1",
"reactify": "^0.15.2",
"watchify": "^3.4.0"
},
"browserify": {
"transform": [
"reactify",
"envify"
]
},
"scripts": {
"start-css": "catw -c 'lessc -' 'css/src/*.less' -o css/bundle.css -v",
"start-js": "watchify -o js/bundle.js -v -d js/app.js",
"start": "npm run start-css & npm run start-js"
},
"author": "Test"
}
Your test uses ES2015, for which you require a transpiler like Babel.
To get this working with Mocha, you first need to install a few packages:
$ npm install --save-dev babel-core babel-preset-es2015 babel-preset-react
Next, add the following to your package.json:
"babel": {
"presets": [ "es2015", "react" ]
}
Or, if you don't have a package.json, create a file called .babelrc in the working directory, containing the following:
{
"presets": [ "es2015", "react" ]
}
Lastly, tell Mocha to use the Babel transpiler:
$ mocha --compilers js:babel-core/register test.js

Resources