Lesson 2
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.
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:
node --version
npm --versionEach 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.

Create a project folder, initialize it, then install TypeScript as a development dependency:
mkdir my-ts-project
cd my-ts-project
npm init -y
npm install --save-dev typescriptmkdir 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:
npx tsc --versionCreate a hello.ts file:
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.
npx tsc hello.ts
node hello.jsThe 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.

For a real project, you configure the compiler in a tsconfig.json file. Generate it with:
npx tsc --initThe 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:
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"strict": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"]
}true.src.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.
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:
{
"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.
tsc without npx: if TypeScript is only installed in the project, the terminal doesn't know the tsc command.npm install would then install TypeScript in the wrong place. Always check that you are in the right folder with cd.src folder: with "include": ["src"], the compiler finds no files to compile and reports it.dist: your changes would be overwritten at the next compilation. Always edit the .ts files.strict to make an error go away: the error hasn't disappeared, it is just hidden. Fix the code instead.tsc compiler turns .ts files into .js files.tsconfig.json file configures the compiler, and strict should stay enabled.src, the output in dist.Exercise
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.