A JavaScript module can have one default export, its main value. export default marks it, and another file imports it without curly braces, under any name it likes.
Every command and output on this page is real, run with Node.js 26.13.1 on macOS.
Syntax
export default value;
import name from "./file.js";
export default value: makesvalue, such as a function, the file’s default export. A file can have only one.import name from "./file.js": imports that default export from another file.nameis any name you choose, written without curly braces."./file.js": the path to the file, starting with./for the same folder.
See export (opens in a new tab) and import (opens in a new tab) on MDN.
Prerequisites
- Node.js, to run the example. Install it from the Node.js download page (opens in a new tab).
The Example
Two files in the same folder: greet.js exports a greeting function as its default, and app.js imports it and calls it.
Step 1: Export the Function
export default function greet(name) {
return "Hello, " + name + "!";
}
export default in front of the function makes it the file’s main value.
Step 2: Import It
import greet from "./greet.js";
console.log(greet("Sam"));
The import has no curly braces, and the name greet is chosen here.
Step 3: Run It
node app.js
Hello, Sam!
app.js called the function it imported from greet.js.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Importing a default export with curly braces: import { greet } | SyntaxError: The requested module './greet.js' does not provide an export named 'greet' | Import it without braces: import greet from "./greet.js" |
Writing export default twice in one file | SyntaxError: Identifier '.default' has already been declared | Keep one default export, and use named exports for the rest |
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
A warning that mentions MODULE_TYPELESS_PACKAGE_JSON | The folder has a package.json without a "type" field | Add "type": "module" to it, as the warning says |
SyntaxError: Cannot use import statement outside a module | The package.json says "type": "commonjs" | Change it to "type": "module" |
FAQ
Can a file have a default export and named exports?
Yes. Import both in one line: the default first, then the named ones in curly braces.
Conclusion
export default marks a file’s one main value, and import name from brings it in, without braces and under any name.
Additional Resources
- export (opens in a new tab) and import (opens in a new tab) (MDN)
- JavaScript modules (opens in a new tab) (MDN)
- Modules: ECMAScript modules (opens in a new tab) (Node.js docs)
JavaScript is a trademark of Oracle Corporation in the United States. Node.js is a trademark of the OpenJS Foundation.
0 comments