Sub topic 4
Installing TypeScript in a project
This chapter assumes you can use a terminal, have Node.js and npm installed, and know what package.json is. You don't need to know any TypeScript syntax yet. By the end you will have the compiler installed in a project, know where it lives on disk, and be able to diagnose the usual installation problems.
What you are installing
TypeScript is distributed as one npm package called typescript. It contains two things you will use:
tsc, the command-line compiler. It checks types and writes JavaScript files.- The language service, a library that editors such as VS Code use for completions, error squiggles, and rename refactoring.
Installing the package gives you both. The compiler is itself written in TypeScript and runs on Node.js, so the only runtime requirement is a reasonably current Node.js. Run node --version first. If Node is very old, the install may succeed and tsc may then fail with a syntax error from inside the compiler. Updating Node fixes this.
Local install versus global install
You can install the compiler in two places.
| Row | Local (per project) | Global (per machine) |
|---|---|---|
| Command | `npm install --save-dev typescript` | `npm install --global typescript` |
| Location | `node_modules` in the project | The npm global prefix |
| Version recorded in | `package.json` and the lockfile | Nowhere |
| Teammates and CI get the same version | Yes | Only if everyone installs it by hand |
| Two projects can use different versions | Yes | No |
Use a local install for any project you share or deploy. TypeScript releases sometimes add checks that flag code that used to compile cleanly. A project that pins its compiler upgrades on purpose. With a global install, the project's behavior changes whenever someone on the team updates their machine.
A global install is acceptable for a throwaway experiment. It is not a good basis for a project.
Installing the compiler step by step
Start from a project directory that has a package.json. If you are starting from nothing, create one:
mkdir ts-demo && cd ts-demo
npm init -y
npm install --save-dev typescript
npx tsc --versionHere is what each line does:
mkdir ts-demo && cd ts-democreates the project folder and moves into it.npm init -ywrites a defaultpackage.jsonand accepts every default answer.npm install --save-dev typescriptdownloads the compiler intonode_modulesand records it underdevDependencies. The short form isnpm install -D typescript.npx tsc --versionruns the local compiler and prints its version, for exampleVersion 5.6.3. Your number will differ.
The last command is your smoke test. If it prints a version, the install worked.
Why `--save-dev`
The compiler is a development tool. The code that runs in production is the JavaScript it produces, and that JavaScript does not import typescript. Listing the package under devDependencies states this. Production installs that use npm install --omit=dev skip it, which keeps deployed images smaller.
There is one exception. If your running application imports the compiler as a library, for example to transform code at runtime, it belongs under dependencies. This is rare.
What changed on disk
After the install, package.json has a new section:
{
"name": "ts-demo",
"version": "1.0.0",
"devDependencies": {
"typescript": "^5.6.3"
}
}The caret in ^5.6.3 means npm may install any later 5.x.y version, but never 6.0.0. TypeScript does not follow strict semantic versioning. A minor release such as 5.7 can introduce new type errors in existing code. The caret range is therefore looser than it looks.
The exact installed version is recorded in package-lock.json. That file is what makes installs repeatable. Commit it.
You will also see:
node_modules/typescript/, which holds the compiler's code. Do not commit this folder. Addnode_modulesto.gitignore.node_modules/.bin/tsc, a small launcher that points at the compiler. This is the filenpxlooks for.
If you want exact pinning, install with npm install --save-dev --save-exact typescript. That writes "typescript": "5.6.3" with no caret. Teams that want upgrades to appear as deliberate pull requests often do this.
How `npx tsc` finds the compiler
npx is a runner that comes with npm. When you type npx tsc, it looks for an executable named tsc in the local node_modules/.bin folder, and uses it if found.
The third branch is a trap. On the npm registry, the package named tsc is not the TypeScript compiler. The compiler's package is typescript, which happens to provide a command called tsc. If you run npx tsc in a project with no local TypeScript, npx can offer to download the unrelated tsc package. Decline the prompt and install typescript instead.
For a one-off run without installing anything, name the package explicitly:
npx --package typescript tsc --versionThis downloads typescript into a temporary cache and runs its tsc command. Nothing is added to your project.
Using the compiler through npm scripts
Typing npx tsc works, but teams usually wrap commands in package.json scripts. Inside a script, npm puts node_modules/.bin on the path for you, so you write tsc with no npx.
{
"name": "ts-demo",
"version": "1.0.0",
"scripts": {
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"typescript": "^5.6.3"
}
}Run it with npm run typecheck. That script needs a tsconfig.json to know which files to check. If you run tsc with no files and no config, the compiler just prints its help text. You will create that config in a later chapter.
Scripts give everyone the same command names. CI can call npm run typecheck without knowing how the compiler is invoked.
Other package managers
The idea is identical in every package manager: add typescript as a dev dependency and run the local binary.
| Tool | Install | Run the compiler |
|---|---|---|
| npm | `npm install --save-dev typescript` | `npx tsc` |
| pnpm | `pnpm add --save-dev typescript` | `pnpm exec tsc` |
| Yarn | `yarn add --dev typescript` | `yarn tsc` |
Use whichever tool matches the lockfile already in the repository. Mixing them creates two competing lockfiles.
Reproducible installs in CI
On a build server, use npm ci instead of npm install. It installs exactly what package-lock.json lists and fails if the lockfile and package.json disagree. This guarantees that CI runs the same compiler version as your machine.
npm ci
npx tsc --versionPrinting the version early in a CI log is a cheap way to diagnose "works locally, fails in CI" reports.
Keeping the editor and the command line in agreement
The editor and the command line can use different compilers.
- VS Code ships its own copy of TypeScript. By default, the squiggles you see come from that bundled version.
npx tscuses the version in your project.
When these differ, the editor may show an error that tsc doesn't report, or the reverse. In VS Code, open a TypeScript file, open the command palette, run "TypeScript: Select TypeScript Version", and choose "Use Workspace Version". The editor then uses node_modules/typescript. Other editors have equivalent settings, usually under "language server" or "TypeScript SDK".
Common problems
tsc: command not found. You typedtscdirectly in the shell, and the compiler is installed locally. Usenpx tscor an npm script.npm ERR! enoentaboutpackage.json. You ran the install in a folder with nopackage.json. Runnpm init -yfirst, orcdto the project root.- A different version than expected. Run
npx tsc --versionand compare it withdevDependencies. If the numbers disagree, deletenode_modulesand runnpm ci. - The install happened in a parent folder. In monorepos, running
npm installin the wrong directory adds the dependency to the wrongpackage.json. Check whichpackage.jsonchanged. - Peer dependency warnings. Some tools, such as linters and test frameworks, declare which TypeScript versions they support. If npm warns about a range, believe it. Use a version inside that range.
Checking your understanding
Exercise
A teammate says npx tsc --version prints 5.6.3 on their machine, but it prints 5.4.5 on yours. Both of you cloned the same repository, and package.json lists "typescript": "^5.6.3". Which commands do you run to find the cause and fix it, and why did the mismatch likely happen?
Show answer
Run npm ls typescript to see which copy npx resolves to, then npm ci to reinstall exactly what package-lock.json specifies. The mismatch likely happened because node_modules was installed before the lockfile or package.json changed, so your folder is stale. A global tsc earlier on your PATH is another possible cause, which is why you check where the version comes from before reinstalling.
Exercise
Why is npm install --save-dev typescript preferred over npm install --global typescript for a project shared by a team?
Show answer
The local install records the compiler version in package.json and package-lock.json, so every teammate and the CI server get the same version. A global install is stored outside the project, is not recorded anywhere, and is shared by every project on the machine.