Babel: Unexpected Token (e.g., import) Error – Detailed Explanation and Solution
What is the “Unexpected Token” Error in Babel?
When you see an error like:
SyntaxError: Unexpected token 'import'
or
Module parse failed: Unexpected token (1:0)
it usually means that Babel is not correctly transpiling modern JavaScript (ES6+) code, such as import/export
statements, into a format that your runtime environment (like Node.js or a browser) can understand.
Why Does This Error Happen?
The error occurs because of one or more of the following reasons:
- Babel is not installed or configured properly
- If Babel isn’t set up correctly in your project, it won’t transform ES6+ syntax.
- Incorrect Babel Presets or Plugins
- Babel requires certain presets like
@babel/preset-env
or@babel/preset-react
to handle modern JavaScript and JSX.
- Babel requires certain presets like
- Babel is not being applied to
node_modules
- By default, Babel doesn’t transpile
node_modules
, so dependencies using modern JavaScript may cause this error.
- By default, Babel doesn’t transpile
- Using ES Modules (
import/export
) in a Node.js environment that doesn’t support them- Node.js (prior to v12) doesn’t support ES modules unless you explicitly enable them.
- Incorrect Webpack Configuration
- If Webpack is not set up to use Babel properly, the import/export statements won’t be transpiled.
Step-by-Step Fixes
Step 1: Check Babel Installation
Make sure Babel is installed in your project by running:
npm list --depth=0 | grep babel
or for Yarn:
yarn list --depth=0 | grep babel
If Babel is missing, install it using:
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-react @babel/plugin-transform-runtime
For TypeScript projects, also install:
npm install --save-dev @babel/preset-typescript
Step 2: Configure Babel Properly
Ensure you have a .babelrc
or babel.config.json
file in your project root with the following content:
For General JavaScript (React and Modern ES6+ JavaScript)
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": ["@babel/plugin-transform-runtime"]
}
For TypeScript
{
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"]
}
Step 3: Ensure Babel is Used in Webpack
If you’re using Webpack, make sure Babel is correctly set up in webpack.config.js
:
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
};
If using JSX or TypeScript:
module.exports = {
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
}
};
Ensure babel-loader
is installed:
npm install --save-dev babel-loader
Step 4: Allow Babel to Process node_modules
(if needed)
By default, Babel doesn’t transpile code inside node_modules
, which may cause issues if you’re using a package with modern JavaScript syntax.
To fix this, modify webpack.config.js
:
module.exports = {
module: {
rules: [
{
test: /\.js$/,
include: /node_modules\/some-es6-library/,
use: {
loader: "babel-loader"
}
}
]
}
};
Replace some-es6-library
with the actual package name.
Step 5: Use CommonJS Instead of ES Modules (For Node.js)
If you’re running a Node.js application and see this error, try using CommonJS instead of ES modules:
const myModule = require("./myModule"); // CommonJS
If you still want to use ES Modules, make sure:
- Your
package.json
has:{ "type": "module" }
- You run the script with:
node --experimental-modules index.mjs
Step 6: Restart Your Development Server
After making changes, restart your server:
npm run dev
or
yarn start
If using Webpack, rebuild your project:
npm run build
Final Thoughts
- Double-check your Babel installation.
- Ensure Babel is configured correctly with the right presets.
- Make sure Webpack (if used) is processing files through Babel.
- Enable CommonJS if running Node.js.
- Restart the development server after making changes.
By following these steps, the “Unexpected token ‘import'” error should be resolved, and your JavaScript project will work as expected.