Lesson 2 / 1217%

Lesson 2

Installation and Configuration

15 min

Install TypeScript with npm, compile a file and configure tsconfig.json.

Definition

A package manager is a tool that downloads, installs and updates the libraries a project needs. The one for Node.js is called npm (Node Package Manager). Each installed library is called a package, or a dependency of the project.

A development dependency is a package that is only useful while you build the project, not when the application runs on the user's side. TypeScript is a good example: it is used to check and compile the code, then disappears from the final result. You install it with the --save-dev option.

Prerequisite: Node.js

TypeScript is installed through npm, the package manager that comes with Node.js. Download Node.js (LTS version) from nodejs.org.

The LTS (Long Term Support) version is the one that receives fixes for several years: it is the most stable, and the one recommended for learning. Here, Node.js is the environment that runs JavaScript outside the browser; it is what will run the code produced by the TypeScript compiler.

Then check the installation in a terminal:

terminal
node --version
npm --version

Each command prints a version number, for example v22.11.0 for Node.js. If the terminal says the command was not found, Node.js is not installed properly: close and reopen the terminal, and redo the installation if needed.

Screenshot of a terminal showing the Node.js and npm versions.

Installing the TypeScript compiler

Create a project folder, initialize it, then install TypeScript as a development dependency:

terminal
mkdir my-ts-project
cd my-ts-project
npm init -y
npm install --save-dev typescript
  • mkdir creates the project folder, and cd moves into it. All the following commands run from there.
  • npm init -y creates a package.json file, the project's identity card. The -y option accepts all the default values without asking questions.
  • npm install --save-dev typescript downloads TypeScript into a node_modules folder and adds it to the devDependencies section of package.json.

Why install TypeScript in the project rather than once on the computer? Because each project can then use its own version, and anyone who gets your project only has one command to run to obtain exactly the same tool. The node_modules folder can be large: you don't share it, it is recreated with npm install.

The compiler is called tsc. You run it with npx so that it uses the version installed in the project:

terminal
npx tsc --version

Writing and compiling a first file

Create a hello.ts file:

hello.ts
const message: string = "Hello TypeScript!";
console.log(message);

This code declares a constant message of type string, then prints it to the console. This is the first time you use a type annotation: the : string after the variable name.

terminal
npx tsc hello.ts
node hello.js

The first command generates a hello.js file, and the second runs it with Node.js.

Open the hello.js file: you will find your code in it, without the : string annotation. The compiler simply removed the types, as we saw in the introduction. Also note that when you pass a file directly to tsc, it ignores the tsconfig.json file: this is handy for a quick test, but not for a real project.

Screenshot of a terminal showing the compilation of hello.ts and the execution of hello.js.

The tsconfig.json file

For a real project, you configure the compiler in a tsconfig.json file. Generate it with:

terminal
npx tsc --init

The generated file contains many options, most of them commented out: it is a catalog of everything you can adjust. To get started, you only keep a few of them.

The most useful options for beginners:

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "strict": true,
    "outDir": "./dist",
    "rootDir": "./src"
  },
  "include": ["src"]
}
  • strict: enables all the strict checks. This is the recommended setting, so always keep it set to true.
  • outDir and rootDir: where your sources are and where the JavaScript is generated.
  • target: the version of JavaScript produced.
  • include: the list of folders the compiler should examine. Here, only src.
  • module: the module system used in the produced JavaScript. ESNext corresponds to modern import and export.

Once the file is configured, a simple npx tsc compiles the whole project, and npx tsc --watch recompiles automatically every time you save.

Organizing the project

With this configuration, the project naturally takes the following shape:

  • src/ contains your TypeScript files: this is where you work.
  • dist/ contains the generated JavaScript. You never touch it by hand: it is recreated on every compilation.
  • node_modules/ contains the packages installed by npm.
  • package.json and tsconfig.json describe the project and its configuration.

To avoid retyping the commands, you can store them in the scripts section of package.json:

package.json (excerpt)
{
  "scripts": {
    "build": "tsc",
    "dev": "tsc --watch"
  }
}

You can then run npm run build to compile once, or npm run dev to recompile on every save.

Good to know

VS Code already bundles TypeScript: you get errors and autocompletion without configuring anything. Installing tsc is for generating the JavaScript. Frameworks like Vite or Astro handle that step for you.

Even when the code contains type errors, tsc prints the errors but still generates the JavaScript, unless you configure it otherwise. So don't rely on the presence of the generated file: always read the compiler's messages.

Common mistakes to avoid

  • Typing tsc without npx: if TypeScript is only installed in the project, the terminal doesn't know the tsc command.
  • Running commands outside the project folder: npm install would then install TypeScript in the wrong place. Always check that you are in the right folder with cd.
  • Forgetting to create the src folder: with "include": ["src"], the compiler finds no files to compile and reports it.
  • Editing the files in dist: your changes would be overwritten at the next compilation. Always edit the .ts files.
  • Turning off strict to make an error go away: the error hasn't disappeared, it is just hidden. Fix the code instead.

Key takeaways

  • TypeScript is installed with npm, which comes with Node.js.
  • The tsc compiler turns .ts files into .js files.
  • The tsconfig.json file configures the compiler, and strict should stay enabled.
  • Sources go in src, the output in dist.

Exercise

Create the my-ts-project folder, install TypeScript, generate atsconfig.json, then create src/index.ts that prints a message. Compile with npx tsc and run the generated file in dist. To go further, add the two scripts build and dev to package.json, then run npm run dev and edit your file to see the automatic recompilation.