Today you will learn:
By the end, you will structure projects like a professional developer.
Export functions, objects, or variables from one file and import in another:
// math.js
export const add = (a, b) => a + b;
// main.js
import { add } from './math.js';
console.log(add(2, 3)); // 5
Use Webpack or Vite to bundle multiple JS files into one:
npm install vite or npm install webpack)npm run build to produce bundleEnsure code quality:
eslint to find potential problemsRemove unused imports automatically during bundling:
import { add, subtract } from './math.js';
// only 'add' is used
console.log(add(2,3));
// math.js
export const add = (a, b) => a + b;
// main.js
import { add } from './math.js';
console.log(add(2, 3));
Complete the following:
Example:
// utils.js
export function greet(name) {
return `Hello ${name}`;
}
// main.js
import { greet } from './utils.js';
console.log(greet("Alex")); // Hello Alex