Learn how to separate your code into different files and import functions from one file into another using Node.js modules. This helps you organize your code and reuse functions across multiple files.
JavaScript has two main ways to import and export code:
1. CommonJS (older style):
// Export
module.exports = add;
// Import
const add = require('./add');
2. ES6 Modules (modern style):
// Export
export default add;
// Import
import add from './add.js';
In this lesson, weβll use ES6 modules (the modern style). This requires adding "type": "module" to your package.json file, or using .mjs file extensions.
When your code gets bigger, itβs better to organize it into separate files:
Before you can import a function, you need to export it from the file where itβs defined.
add.js// Your function code here...
// Export the function so other files can use it
export default add;
The export default line makes the function available to other files.
Note: The add function is just a simple example to demonstrate the import/export pattern. In real projects, youβd use more useful functions like:
toSnakeCase() β Converting strings to snake_case (from the previous lesson)formatDate() β Formatting datesvalidateEmail() β Validating email addressescalculateTotal() β Business logic calculationsThe import/export pattern works the same way regardless of what your function does!
When your files are in the same directory (next door to each other), you use a relative path starting with ./.
my-project/
βββ add.js
βββ app.js
add.js (the function file):// Your function code here...
export default add;
app.js (the file that uses the function):// Import the function from the same directory
import add from './add.js';
// Now you can use it!
let result = add(2, 3);
console.log(result); // 5
Key points:
./ means βcurrent directoryβimport add from './add.js' looks for add.js in the same folder.js extension in ES6 modulesWhen your function is in a subdirectory (a folder inside your project), you need to specify the path with ./ followed by the folder name.
my-project/
βββ app.js
βββ helpers/
βββ math.js
helpers/math.js (the function file):// Your function code here...
export default add;
app.js (the file that uses the function):// Import from the helpers subdirectory
import add from './helpers/math.js';
// Now you can use it!
let result = add(5, 7);
console.log(result); // 12
Key points:
./helpers/math.js means βlook in the helpers folder for math.jsβ./ when itβs a relative path./utils/helpers/math.jsThere are different ways to specify where to find a module:
| Path Type | Example | Meaning |
|---|---|---|
| Relative | ./add |
Same directory |
| Relative | ./helpers/math |
Subdirectory |
| Relative | ../utils/helper |
Parent directory (go up one level) |
| Absolute | /Users/name/project/helper |
Full path from root |
| Node Module | fs or path |
Built-in Node.js module |
For now, focus on relative paths starting with ./ for files in your project.
Letβs see a complete example with multiple files:
my-project/
βββ app.js
βββ helpers/
βββ math.js
helpers/math.js:// Your function code here...
// Export both functions
export { add, subtract };
app.js:// Import both functions from the helpers folder
import { add, subtract } from './helpers/math.js';
// Use them
console.log(add(5, 3)); // 8
console.log(subtract(10, 4)); // 6
Note: When exporting multiple functions, you use export { add, subtract } and can import specific ones using destructuring { add, subtract }.
helpers/ folderhelpers/string-convert.js with your toSnakeCase function from the previous lessontoSnakeCase functionapp.js to convert βHello Worldβ to snake casetoKebabCase and toCamelCase to your helpers/string-convert.js fileapp.jsutils/helpers/ structurestring-convert.js file theretoSnakeCase using ./utils/helpers/string-convert| Concept | Description | Example |
|---|---|---|
| Export | Making a function available to other files | export default add; |
| Import (Same Dir) | Getting a function from the same folder | import add from './add.js'; |
| Import (Subdirectory) | Getting a function from a subfolder | import add from './helpers/math.js'; |
| Multiple Exports | Exporting multiple functions | export { add, subtract }; |
| Relative Path | Path starting with ./ thatβs relative to current file |
./helpers/math.js |
| Module | A file that exports functions/variables for reuse | math.js |
This lesson was created with assistance from GPT-5.