This webpack error occurs when files containing JSX, special syntax, or binary content are processed without the appropriate loader configured. The bundler attempts to parse the file as plain JavaScript but encounters characters it cannot understand.
This error indicates that webpack encountered a file it cannot parse because no appropriate loader is configured to handle its content. The "unexpected character" is typically JSX syntax (like '<'), special characters from CSS/SCSS files ('@'), private class fields ('#'), or binary content from non-JavaScript files. Webpack needs explicit instructions (loaders) to transform different file types into JavaScript that browsers can understand. When it tries to parse a file without the right loader, it treats the content as plain JavaScript and fails when it encounters syntax or characters that aren't valid JavaScript. In React projects, this most commonly happens when JSX files aren't being processed by babel-loader, or when third-party dependencies use modern JavaScript features that aren't being transpiled properly.
Check the error message to see which file is causing the issue:
Module parse failed: Unexpected character '<' (1:0)
File was processed with these loaders:
* ./node_modules/some-package/file.jsNote the file path and the unexpected character. Common characters indicate:
- '<' = JSX syntax
- '@' = CSS/SCSS syntax
- '#' = Private class fields
- '�' = Binary file content
If the error involves JSX (unexpected '<'), ensure your webpack config includes babel-loader:
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react']
}
}
}
]
}
};For TypeScript projects, add tsx support:
test: /\.(ts|tsx)$/,
use: 'ts-loader'If the error comes from a third-party package, update it to a version that's properly transpiled:
npm update package-name
# or
npm install package-name@latestFor Create React App projects, update react-scripts:
npm install react-scripts@latestThen clear cache and reinstall:
rm -rf node_modules package-lock.json
npm installIf importing CSS, images, or other assets, ensure loaders are configured:
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource'
}
]
}
};Install required loaders:
npm install --save-dev style-loader css-loaderIf a specific package in node_modules needs transpilation, modify the exclude rule:
{
test: /\.(js|jsx)$/,
exclude: /node_modules\/(?!(problematic-package)\/).*/,
use: 'babel-loader'
}Or for Create React App without ejecting, create a .babelrc in the package causing issues, or report the issue to the package maintainer.
For Create React App users: CRA abstracts webpack configuration, so you typically can't modify loaders without ejecting. If you encounter this error with CRA:
1. First try updating react-scripts to the latest version
2. Check if the problematic dependency has a newer version
3. As a last resort, use react-app-rewired or CRACO to override configuration without ejecting
4. Only eject if you absolutely need custom webpack configuration
Private class fields (#): Modern JavaScript features like private fields may not be supported by older versions of babel-loader or @babel/preset-env. Ensure you're using:
- @babel/preset-env with targets configured
- @babel/plugin-proposal-class-properties (or @babel/plugin-proposal-private-methods)
Webpack 5: If using Webpack 5, some loader syntax has changed. For example, file-loader and url-loader are replaced by Asset Modules (type: 'asset/resource').
Monorepo considerations: In monorepo setups, you may need to explicitly include workspace packages in babel-loader rules since they're technically in node_modules but should be transpiled.
Prop spreading could cause security issues
Prop spreading could cause security issues
Error: error:0308010C:digital envelope routines::unsupported
Error: error:0308010C:digital envelope routines::unsupported
React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render.
React Hook useEffect placed inside a condition
Hook can only be called inside the body of a function component
Hook can only be called inside the body of a function component
Rollup failed to resolve import during build
How to fix "Rollup failed to resolve import" in React