Warming up the neural circuits...
As your codebase grows, putting everything in one file becomes unmanageable. Modules are a way to split your code into separate files, each with its own responsibility.
// Without modules: one huge file
// 5000 lines of code — find what you need? Good luck!
// With modules: organized by purpose
// 📁 src/
// ├── user.js (user-related functions)
// ├── api.js (API calls)
// ├── components/ (UI components)
// └── utils.js (helper functions)The modern module system uses export and import.
// 📁 utils.js
export function formatDate(date) { ... }
export function capitalize(str) { ... }
export const MAX_SIZE = 100;
// 📁 app.js
import { formatDate, capitalize, MAX_SIZE// 📁 user.js
export default class User {
constructor(name) { this.name = name; }
}
// 📁 app.js
import User from "./user.js"; // No curly braces!// 📁 math.js
export default function add(a, b) { return a + b; }
export const PI = 3.14159;
export function multiply(a, b) { return a *
import { formatDate as format } from "./utils.js";
import * as Utils from "./utils.js";
Utils.formatDate(new Date());npm is the world's largest package registry. It lets you install and use code written by other developers.
npm init -y
# Install a package
npm install lodash
npm install react react-dom
# Install as dev dependency (for build tools, not production)
npm install --save-dev vite
# Install a specific version
npm install lodash@4.17.21
# Update all packages
npm update
# Remove a package
Every Node.js project starts with a package.json file:
{
"name": "my-project",
"version": "1.0.0",
"description": "A sample project",
"main": "index.js",
"type": "module",
"scripts"
"react": "^18.2.0"
│││
││└── Patch (bug fixes, no breaking changes)
│└── Minor (new features, no breaking changes)
└── Major (breaking changes)^18.2.0 = Accept minor and patch updates (18.x.x)~18.2.0 = Accept only patch updates (18.2.x)"18.2.0" = Exactly this version (no updates)Modern JavaScript development uses build tools to:
<script src="lib1.js"></script>
<script src="lib2.js"></script>
<script src="component1.js"></script>
<script src="component2.js"></script>
<
<script src="bundle.js"></script>
<!-- 1 HTTP request! Optimized! ES modules work! -->Vite is the modern, fast build tool used by React, Vue, and Svelte.
# Create a new Vite project
npm create vite@latest my-app -- --template vanilla
# Or for React: --template react
cd my-app
npm install
npm run dev # Start dev server (usually http://localhost:5173)my-app/
├── index.html # Entry HTML file
├── package.json # Dependencies and scripts
├── vite.config.js # Vite configuration
├── public/ # Static assets
│ └── favicon.svg
└── src/
├── main.js # Entry JavaScript file
├── style.css # Styles
├── counter.js # Module
└── utils/
└── helpers.js # Another moduleimport { defineConfig } from 'vite';
export default defineConfig({
root: '.',
build: {
outDir: 'dist',
minify: 'esbuild',
},
server: {
port:
| Package | Purpose | When to Use |
|---|---|---|
lodash | Utility functions | Data manipulation, deep cloning |
date-fns | Date formatting | Working with dates |
axios | HTTP client | calls (alternative to fetch) |
zod | Schema | Runtime type checking |
tailwindcss | framework | Styling (you already know this!) |
react / react-dom | UI framework | Building component UIs |
express | Server framework | Backend APIs |
vitest | Testing framework | Unit tests |
eslint | Code linting | Enforcing code quality |
Browser JS Node.js
Entry HTML file Terminal
Import import from './x.js' import from 'fs'
APIs DOM, fetch, console fs, http, process
Global window, document global, process
Purpose User interface Server, CLI, toolingBrowser: import { something } from "./module.js"
Node.js: import fs from "fs" (built-in) or import express from "express" (npm package)
export / import with named and default exportsThis concludes the JavaScript Fundamentals track! 🎉
Next up: