score:1
Modify your babel-loader config:
- Change your
test
condition to/\.jsx?$/
, so .jsx will be also transpiled. - Add
options
with yourreact
preset under(!)use
(not as sibling touse
) - If you want to use Babel 6, then you need to install babel-loader v7 (
npm i -D babel-loader@7
) instead of v8 which is for Babel 7.
Then you should be fine. Here are the relevant parts of configs for Babel 6 (and 7 if you want to change) that worked for me:
Babel 6
webpack:
module.exports = {
...
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
options: {
presets: ["react"]
},
}
}
]
}
};
package.json:
{
...
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1"
},
}
Babel 7
webpack:
module.exports = {
...
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-react"]
}
}
}
]
}
};
package.json:
{
...
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.6"
}
}
Hope, it helps.
score:1
It means your browser is serving un-transpiled or un-transformed code.
This issue comes when somehow those files are left out for the transpilation, please make sure those folders are included in webpack module section under a valid loader, for example - for typescript - ts-loader etc. Spent a lot of hours and got the fix.
You may use another compiler like babel for the same. It's up to you what suits your need.
module: {
rules: [
{
test: /\.ts|\.tsx?$/, loader: 'ts-loader',
options: { onlyCompileBundledFiles: true, configFile: "testing.tsconfig.json", compilerOptions: {
noEmit: false, allowTsInNodeModules: false
}},
include: [
path.resolve(__dirname, "./src"),
path.resolve(__dirname, "./tests")
],
exclude: [
path.resolve(__dirname, './automation/'),
path.resolve(__dirname, './src/types/testutils/'),
path.resolve(__dirname, './node_modules/')
]
}
]
}
Look at this include section:
include: [
path.resolve(__dirname, "./src"),
path.resolve(__dirname, "./tests")
],
score:2
You need to add the preset for react to be loadable via babel, and add support for JSX files
module.exports = {
entry: path.resolve(__dirname, 'src/App.js'),
watch: true,
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [{
test: /\.jsx?$/, // change here
exclude: /(node_modules|bower_components)/,
options: { // added this
presets: ['react'],
},
use: {
loader: 'babel-loader',
}
]
},
}
Source: stackoverflow.com
Related Query
- ERROR in ./src/index.js 14:4 Module parse failed: Unexpected token (14:4) You may need an appropriate loader to handle this file type
- Module parse failed: Unexpected token (7:5) You may need an appropriate loader to handle this file type
- ERROR in ./src/index.js Module parse failed:Unexpected token (13:16) You may need an appropriate loader to handle this file type
- Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type. <!DOCTYPE html>
- Module parse failed: Unexpected token (41:41) You may need an appropriate loader to handle this file type
- React Failed to compile error: Module parse failed: Unexpected token. You may need an appropriate loader to handle this file type
- How to fix error "Failed to compile : ./node_modules/@react-leaflet/core/esm/path.js 10:41 Module parse failed: Unexpected token (10:41)"
- React - Module parse failed: Unexpected Token. You may need an appropriate loader to handle this file type
- Module parse failed: Unexpected character '�' (1:0) You may need an appropriate loader to handle this file type
- React / Webpack - "Module parse failed: Unexpected token - You may need an appropriate loader to handle this file type."
- Getting Module parse failed: Unexpected token (1:0) error when I try to import css file for a specific component in react
- Module parse failed: Unexpected token m in JSON at position 0
- Webpack module build failed unexpected token (rails react build)
- ERROR in ./index.js Module build failed: SyntaxError: Unexpected token
- Next.js Import image error Module parse failed: Unexpected character '�' (1:0)
- React - Module parse failed: Unexpected token
- Module parse failed: Unexpected character '@'You may need an appropriate loader to handle this file type.(Webpack : 4.4.0)
- Webpack React error: Module parse failed: Unexpected token
- Module parse failed: Unexpected token - react-refresh-webpack-plugin/loader
- React and Webpack Module parse failed: /testimonials.js Unexpected token (6:4)
- Error Module build failed: SyntaxError: Unexpected token
- Module parse failed, unexpected token with Webpack
- webpack tsx Module parse failed: Unexpected token
- Failed to compile Module parse failed: Unexpected character, when importing or accessing docx file reactjs
- Module parse failed: Unexpected token (1:68)
- React JS: ERROR in index.css 1:0 (Module parse failed: Unexpected token (1:0))
- Error in react-toastify/dist/ReactToastify.css Module parse failed .Toastify__toast-container { | React SSR
- Module parse failed: Unexpected token (6:16) when building react with webpack
- React and NextJS: Module parse failed: Unexpected token
- ERROR in ./app/main.js Module build failed: SyntaxError: Unexpected token (10:18)
More Query from same tag
- Styled Components - passing object as second argument for colour function from polished
- Passing a union type into React's useRef doesn't work
- React 18 addEventListent('click') triggers on a click that happens before component render
- amp form not submitting form data
- Render 'async' function return value in render method (React)
- React-testing-library: changes due to input
- React mqtt subscription setState warning
- Action not mapped with mapDispatchtoProps
- passing values dynamically to href attribute
- React: render user input in cards
- React login page
- Modularize routes in react-router
- Nested JSON to HTML Tables - React JS
- TypeError: Cannot read properties of undefined (reading 'length')
- React: Confusion with accessing another components state
- Is it better to use index as key instead of Math.random() in React?
- how i can use Generic type in function body
- why rendering is delayed
- How can I import multiple components from react-bootstrap?
- React app + heroku server not working on production, but working fine in dev
- React Action, Reducer & Connect Syntax
- How do i implement a search bar which fetches information using an API
- doing POST - using custom id in JSON Server
- Hosting nextjs app to IIS as a sub application
- How to correctly wrap few TD tags for JSXTransformer?
- How to get total column using ReactJS?
- Losing pagination on remote data for Material Table React
- How to add event for dangerousSetHtml?
- react is not letting me edit text field
- React setting multiple states dynamically