Using npm to Install TypeScript
You'll learn how to add TypeScript to a project using npm, and understand where the files actually live.
What npm Does
Think of npm as a librarian that fetches books (packages) and organizes them on your shelf. When you install TypeScript, npm downloads it and records that you're using it, so others can recreate your setup.
Installing TypeScript in a Project
Navigate to your project folder in the terminal and run:
npm install --save-dev typescript
The --save-dev flag tells npm: "This is a development tool, not something my app needs to run in production."
What Just Happened?
Three things were created or modified:
-
node_modules/folder — This is where the actual TypeScript compiler code lives. It's like a warehouse filled with all the packages you've installed. You'll see atypescriptfolder inside, plus any dependencies TypeScript itself needs. -
package.json— This file got a new entry under"devDependencies":"devDependencies": { "typescript": "^5.x.x" }This is your shopping list. Anyone else can run
npm installand get the same packages. -
package-lock.json— A detailed receipt of exact versions installed. This ensures consistency across machines.
Why Local Installation Matters
Installing locally (per-project) means each project controls its own TypeScript version. You might use TypeScript 4.9 in one project and 5.3 in another without conflict.
Key Takeaway: Running npm install --save-dev typescript downloads the compiler to node_modules/, records it in package.json as a dev dependency, and ensures your team can replicate your setup exactly.