Yama

Yama

I’ve been trying to connect React to my Phoenix project but continue to get an error. The error says

Error in ./assets/js/app.jsx
Module build failed (from ./node_modules/babel-loader/lib/index.js);
SyntaxError: path -> app.jsx

const HelloReact = () => {
  return <div>Hello World</div>;
}

error points to < in

I feel like this may be a babel problem. I decided to move package.json, webpack.config.js and .babelrc to the top level due to a recommended approach on a guide I was following. Was hoping to get a second look at eyes with how I’ve configured my React. Thank you for all the help.

My .babelrc looks like

{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": ["@babel/plugin-proposal-class-properties", "@babel/plugin-proposal-object-rest-spread"]
}

As for my config file

module.exports = (env, options) => ({
 optimization: {
  minimizer: [
   new UglifyJSPlugin({ cache: true parallel: true, sourceMap: false }),
   new OptimizeCSSAssetsPlugin({})
  ]
 },
entry: {
  app: "./assets/js/app.jsx"
},
output: {
  filename: "app.js",
  path: path.resolve(__dirname, "priv/static/js")
},
module: {
  rules: [
   {
     test: /\.(js|jsx)$/,
     exclude: /node_modules/,
     use: {
       loader: "babel-loader"
     }
  },
  {
    test: /\.css$/,
    use: [MiniCSSExtractPlugin.loader, "css-loader"]
  }
 ]
},
plugins: [
   new MiniCssExtractPlugin({ filename: "../css/app.css"}),
   new CopyWebpackPlugin([{ from: "assets/static/", to: "../" }])
],
resolve: {
  extensions: ["*", ".js", ".jsx"]
}
});

Showing Posts 1 to 10

lucaong

lucaong

Most likely a Babel problem indeed, as it seems it’s not transpiling JSX to JS.

The Babel configuration is always giving me headaches, and right now I am not really sure, but isn’t babel.config.js supposed to replace .babelrc? If I am right about that, your babel.config.js will take precedence, and it does not include necessary presets like @babel/preset-react. That might be what causes JSX not to be transpiled.

wolfiton

wolfiton

If you are new to react use the creat react app it works with npx and the guide can be found here.

https://facebook.github.io/create-react-app/docs/getting-started

Also this is very important https://reactjs.org/blog/2018/10/01/create-react-app-v2.html

kokolegorille

kokolegorille

const HelloReact = () => {
  return <div>Hello World</div>;
}

Is that your file?

I think You miss this…

import React from 'react';

Even if it is not visible, You need to import React before using this code

<div>...</div>

because it uses React.createElement() under the hood

wolfiton

wolfiton

Also can you post your package.json file to see what packages you installed?

wolfiton

wolfiton

Also this tutorial should offer you a complete introduction to react and babel config the right way.

https://www.valentinog.com/blog/babel/

Yama

Yama OP

Hm I’ve been trying to look into babel.config.js and it doesn’t seem to be a requirement. I may be wrong but thank you for replying. Will continue to look about both files.

Yama

Yama OP

Thank you for replying. Actually, it isn’t my first time using React but every time I try to use it for a project, the initial setup is usually a headache when it comes to webpack. Thank you for providing those linkes. I will take a look especially the tutorial I hope will point me in the right direction. As for the packages I installed, it’s

{
  "repository": {},
  "license": "MIT",
  "scripts": {
    "deploy": "webpack --mode production",
    "watch": "webpack --mode development --watch"
  },
  "dependencies": {
    "@babel/plugin-proposal-class-properties": "^7.5.0",
    "@babel/plugin-proposal-object-rest-spread": "^7.5.4",
    "phoenix": "file:deps/phoenix",
    "phoenix_html": "file:deps/phoenix_html",
    "react-router-dom": "^5.0.1"
  },
  "devDependencies": {
    "@babel/core": "^7.5.4",
    "@babel/preset-env": "^7.5.4",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.6",
    "copy-webpack-plugin": "^4.5.0",
    "css-loader": "^2.1.1",
    "mini-css-extract-plugin": "^0.4.0",
    "optimize-css-assets-webpack-plugin": "^5.0.3",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "uglifyjs-webpack-plugin": "^1.2.4",
    "webpack": "4.28.4",
    "webpack-cli": "^3.3.5"
  }
}
Yama

Yama OP

I apologize for not copying the code exactly as what I wrote. That is my part that has mislead you. The entire file looks like this

import css from "../css/app.css";
import "phoenix_html";
import React from "react";

import ReactDOM from "react-dom";

console.log("I'm getting hit");

const HelloReact = () => {

return <h1>Hello React!</h1>;

};

const root = document.getElementById("root");

ReactDOM.render(<HelloReact />, root);
krstfk

krstfk

I may be wrong, but isn’t the return block supposed to be wrapped with parens ie :

return (<div>Hello react</div>);

?

kokolegorille

kokolegorille

Ok You do import React…

This is how I did for a babel 7.4.3 and React 16.8

I use a babel.config.js with this inside

module.exports = api => {
  api.cache(true);

  const presets = [
    [
      "@babel/preset-env",
      {
        targets: {
          edge: "17",
          firefox: "60",
          chrome: "67",
          safari: "11.1",
        },
        useBuiltIns: "usage",
        corejs: '3.0.0',
      },
      "@babel/preset-react",
    ],
  ];
  
  const plugins = [

  ];

  return {
    presets,
    plugins
  };
}

I think I got this config on the babel site.

You may adapt for your usage.

In webpack.config.js, I have

      {
        test: /\.jsx?$/,
        include: SRC_PATH,
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
          rootMode: "upward",
        },
      },

BTW, as a minor note… I put react, react-dom in dependencies, and all @babel… in devDependencies

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews