babel-loader icon indicating copy to clipboard operation
babel-loader copied to clipboard

JSX is not transpiled in babel-loader when babel/preset-react is specified in .babelrc

Open AbdouTlili opened this issue 5 years ago • 11 comments

i really tried to find the problem before posting here but i couldn't, i hope you can help me

i'm using Babel and webpack in React project and i'm getting this ERROR message

:

zacnnnt@zacnnnt:~/projects/VenvDjango/django-react-Tuto/project$ npm run dev

ERROR in ./project/frontend/src/components/App.js 8:2
Module parse failed: Unexpected token (8:2)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| const App = () => (
| 
>   <DataProvider endpoint="api/lead/" render={data => <Table data={data} />} />
| );
| const wrapper = document.getElementById("app");
 @ ./project/frontend/src/index.js 1:0-35
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] dev: `webpack --mode development ./project/frontend/src/index.js --output ./project/frontend/static/frontend/main.js`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the [email protected] 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!     /home/zacnnnt/.npm/_logs/2019-11-20T21_04_44_609Z-debug.log

App.js

import ReactDOM from "react-dom";
import DataProvider from "./DataProvider";
import Table from "./Table";

const App = () => (

  <DataProvider endpoint="api/lead/" render={data => <Table data={data} />} />
);
const wrapper = document.getElementById("app");
wrapper ? ReactDOM.render(<App />, wrapper) : null;

DataProvider.js

import PropTypes from "prop-types";
class DataProvider extends Component {


  static propTypes = {
    endpoint: PropTypes.string.isRequired,
    render: PropTypes.func.isRequired
  }


  state = {
    data: [],
    loaded: false,
    placeholder: "Loading..."
  }



  componentDidMount() {
    fetch(this.props.endpoint)
      .then(response => {
        if (response.status !== 200) {
          return this.setState({ placeholder: "Something went wrong" });
        }
        return response.json();
      })
      .then(data => this.setState({ data: data, loaded: true }));
  }


  render() {
    const { data, loaded, placeholder } = this.state;
    return loaded ? this.props.render(data) : <p>{placeholder}</p>;
  }
}


export default DataProvider;

AbdouTlili avatar Nov 20 '19 21:11 AbdouTlili

@AbdouTlili can you post your webpack/babel config please?

existentialism avatar Nov 20 '19 21:11 existentialism

@existentialism package.json

{
  "name": "django-react-Tuto",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack --mode development ./project/frontend/src/index.js --output ./project/frontend/static/frontend/main.js",
    "build": "webpack --mode production ./project/frontend/src/index.js --output ./project/frontend/static/frontend/main.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.7.2",
    "@babel/preset-env": "^7.7.1",
    "@babel/preset-react": "^7.7.0",
    "babel-loader": "^8.0.6",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "prop-types": "^15.7.2",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10"
  },
  "dependencies": {
    "weak-key": "^1.0.1"
  }
}

.babelrc

{
    "presets": [
        "@babel/preset-env","@babel/preset-react"
    ],
    "plugins": [
        "transform-class-properties"
    ]
}

webpack.config.js

module.exports = {
    module :{
        rules :[
            {
            test : /\.js$/,
            exclude : /node_modules/,
            use:{
                loader: "bubel-loader"
            }
            }

        ]
    }
};

AbdouTlili avatar Nov 20 '19 21:11 AbdouTlili

loader: "bubel-loader"

Is that a typo?

"transform-class-properties"

You'll want to use the babel 7 compatible version @babel/plugin-proposal-class-properties

existentialism avatar Nov 20 '19 21:11 existentialism

still getting same error message :(

webpack.config.js

module.exports = {
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: "babel-loader"
          }
        }
      ]
    }
};

.babelrc

{
    "presets": [
        "@babel/preset-env","@babel/preset-react"
    ],

    "plugins":[
            "@babel/plugin-proposal-class-properties"
    ]

}

AbdouTlili avatar Nov 21 '19 16:11 AbdouTlili

Do you have multiple package.json? If so, you should use babel.config.json instead of .babelrc (babel.config.* is always recommended if you are not sure about what file you need).

nicolo-ribaudo avatar Nov 21 '19 22:11 nicolo-ribaudo

@AbdouTlili

I did the same tutorial and ran in the same problem. You can solve it by making sure that webpack.config.js and .babelrc are in the top folder of the project (django-drf-react-quickstart).

Hope this helps and have a nice day, Tim

timinho111 avatar Nov 29 '19 12:11 timinho111

Thanks ! I had the same problem, your solution worked just fine :)

albinche avatar Jan 29 '20 14:01 albinche

What is the solution if you are in the mono repo?

I have this structure:

/repo /services /myapp webpack.config.js .babelrc /common

Then when I run under myapp: yarn webpack --config webpack.config.js

I get this error:

ERROR in /Users/me/code/repo/node_modules/@libs/common/index.jsx
Module build failed (from /Users/me/code/repo/node_modules/happypack/loader.js):
SyntaxError: /Users/me/code/repo/node_modules/@libs/common/index.jsx: Unexpected token (8:11)

   6 |     }
   7 | 
>  8 |     return <button type="button" onClick={sayHello}>Click me!</button>;
     |            ^
   9 | };
  10 | 
    at Parser.raise (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:7044:17)
    at Parser.unexpected (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:8422:16)
    at Parser.parseExprAtom (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9701:20)
    at Parser.parseExprSubscripts (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9287:23)
    at Parser.parseMaybeUnary (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9267:21)
    at Parser.parseExprOps (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9137:23)
    at Parser.parseMaybeConditional (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9110:23)
    at Parser.parseMaybeAssign (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9065:21)
    at Parser.parseExpression (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:9017:23)
    at Parser.parseReturnStatement (/Users/me/code/repo/node_modules/@babel/parser/lib/index.js:11091:28)
 @ ./src/index.jsx 13:0-49 29:0-17

simkessy avatar Mar 05 '20 07:03 simkessy

@timinho111 I am going through that tutorial too, and I have the .babelrc and package.json files both in the root folder (django-react for me) and I am getting the same issue.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: `webpack --mode development ./leadmanager/frontend/src/index.js --output ./leadmanager/frontend/static/frontend/main.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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:

Is there anything I should look into??

Westo48 avatar May 21 '20 03:05 Westo48

I have the same error, but with babel-plugin

ERROR in ./src/components/button2.tsx 7:6
Module parse failed: Unexpected token (7:6)
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.
|   children: children,
|   ...rest
> }) => <Component {...rest} size="l" view="action" width="max">{children}</Component>;
 @ ./src/index.tsx 14:0-47 24:36-43

https://github.com/babel/babel/issues/12468

budarin avatar Dec 15 '20 13:12 budarin

It fixed with the latest @babe/*

budarin avatar Dec 19 '20 03:12 budarin