Last updated: Mar 2, 2024
Reading timeยท3 min
The "Uncaught SyntaxError: The requested module does not provide an export named" occurs when mixing up default and named ES6 module imports and exports.
To solve the error make sure to import default exports without using curly braces and import named exports with curly braces.
Here's an example of how the error occurs. This is our index.js
file:
// ๐๏ธ default export export default function sum(a, b) { return a + b; }
And this is a file that imports the function from index.js
.
// โ๏ธ should not use curly braces import {sum} from './index.js'; console.log(sum(10, 15));
Notice that in our index.js
file we used a
default export,
but when we imported the function in the other file we used a named
import.
This is the reason the error occurs.
You can use default imports/exports with ES6 modules in the following way.
// ๐๏ธ default export export default function sum(a, b) { return a + b; }
And import the function using a default import.
// ๐๏ธ default import import sum from './index.js'; console.log(sum(10, 15));
Notice that we do not use curly braces when working with default
imports.
Here's how you use named imports/exports.
// ๐๏ธ named export export function sum(a, b) { return a + b; }
And now we use a named import in the other file.
// ๐๏ธ named import import {sum} from './index.js'; console.log(sum(10, 15));
Notice that we used curly braces, this is how we import a named
export.
You can also mix and match, here's an example.
// ๐๏ธ named export export const num = 100; // ๐๏ธ default export export default function sum(a, b) { return a + b; }
And here are the imports.
// ๐๏ธ default and named imports import sum, {num} from './index.js'; console.log(sum(10, 15)); // ๐๏ธ 25 console.log(num); // ๐๏ธ 100
We used a default import to import the sum
function and a named import to
import the num
variable.
Another common cause of the error is trying to import a function, variable or class from a file that doesn't export it.
Make sure:
default
export per file, but you can have as many
named
exports as necessaryTo solve the error "The requested module does not provide an export named", be consistent with your ES6 imports and exports.
If a value is exported as a default export, it has to be imported as a default import and if it's exported as a named export, it has to be imported as a named import.
You can learn more about the related topics by checking out the following tutorials: