Codectionary / Developer documentation / TypeScript

Install the Node.js prerequisites on Windows, then create, check, compile and run a local TypeScript project in PowerShell.

What you need before starting

TypeScript is source code that a compiler checks and normally turns into JavaScript. For this terminal-based example, Node.js runs the resulting JavaScript and npm installs the TypeScript compiler as a project dependency. Node.js and npm are tools for this workflow; they are not part of the TypeScript language.

First install a currently supported Node.js LTS release for your operating system using the linked Codectionary guide and the official Node.js download page. The installation normally includes npm. Open a new terminal after installing, then check that both commands report versions. The exact version changes over time; a project may also state a required Node range in package.json.

Install Node.js on Windows

Follow the Windows Node.js guide to install the currently supported LTS release from nodejs.org. Open a new PowerShell window after installation. A Node.js installation normally includes npm; verify both before creating the project. If this is a managed computer, use the software installation method your organisation approves.

PowerShell: check the prerequisites

node --version
npm --version

Create a local project

Run these commands in a folder where you can create files. mkdir creates a new directory, and cd enters it. npm init -y writes a starter package.json without asking interactive questions. Review its name and other defaults later; it is the manifest for this project, not a TypeScript source file.

npm install --save-dev typescript adds the compiler to devDependencies in package.json and updates package-lock.json. A local dependency lets the project record its compiler version. npx tsc --version checks the local compiler that the following commands will use. You do not need to install TypeScript globally.

Create the project and install TypeScript locally

mkdir typescript-start
cd typescript-start
npm init -y
npm install --save-dev typescript
npx tsc --version

Create your first .ts file

Create a folder named src inside typescript-start. In a text editor, save the code below as src/hello.ts. The : string annotations tell TypeScript that greet receives and returns text. The call passes "TypeScript", and console.log writes the returned greeting when the resulting JavaScript runs.

Use the exact .ts extension, not hello.ts.txt. Keep the terminal inside typescript-start: the next commands look for package.json, tsconfig.json and src/hello.ts there.

Create the source folder

mkdir src

Save as src/hello.ts

function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("TypeScript"));

Initialise and review tsconfig.json

npx tsc --init creates a tsconfig.json in the current project. The generated defaults can change with the installed TypeScript version, so open the file and replace its contents with the small configuration below for this exercise. This is a Node command-line example; a browser or modern module-based application may need different module settings.

include chooses TypeScript files under src. rootDir and outDir keep the source in src and emitted JavaScript in dist. target selects the JavaScript language level; module: CommonJS fits the default npm package created by npm init -y, which has no "type": "module" field. strict enables stronger checks. noEmitOnError stops a failed compilation from writing new output, though it does not remove files emitted by an earlier successful build.

Create the configuration file

npx tsc --init

Replace the generated contents of tsconfig.json

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "CommonJS",
    "rootDir": "src",
    "outDir": "dist",
    "strict": true,
    "noEmitOnError": true
  },
  "include": ["src/**/*.ts"]
}

Check, compile and run

Run the noEmit command first. It reads the project from tsconfig.json, reports type errors and writes no JavaScript. If it succeeds, run the compiler without noEmit to create dist/hello.js. Then Node executes that JavaScript and prints Hello, TypeScript! in your terminal. TypeScript annotations are absent from the emitted file.

Try replacing greet("TypeScript") with greet(42), then run the noEmit command again. The checker should report a type mismatch; restore the string before compiling. Do not pass src/hello.ts as a separate command-line argument here: this example uses --project so the configuration and its include pattern are applied.

Check types without writing files

npx tsc --project tsconfig.json --noEmit

Compile and execute the JavaScript output

npx tsc --project tsconfig.json
node dist/hello.js

Common Windows setup problems

If node is not recognised, reopen PowerShell and check where.exe node; the executable may not be on PATH or an old installation may appear first. If PowerShell blocks npm.ps1 or npx.ps1, try the installed npm.cmd or npx.cmd command shim. On a managed computer, consult your administrator before changing execution policy.

An EBADENGINE error means a package requires a Node version outside the installed range; compare node --version with the error and choose a supported Node release. If dist/hello.js is missing, check that hello.ts was saved inside src, run the compiler again and read its diagnostics. A successful noEmit check intentionally creates no output.

PowerShell shims if script execution is blocked

npm.cmd --version
npx.cmd tsc --version

Where to go next

You now have a repeatable local workflow: edit src/hello.ts, check types, compile, then run dist/hello.js. A real application may add npm scripts and more source files, but the same distinction between checking and execution remains. npm and package.json are explained in the JavaScript project guide.

Syntax

npm install --save-dev typescript
npx tsc --project tsconfig.json
node dist/hello.js

Examples

A first TypeScript program

Save this as src/hello.ts; check and compile it, then run dist/hello.js with Node.js in PowerShell.

function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("TypeScript"));
  1. The function parameter and return type annotations ask the compiler to check that greet receives and returns a string. They do not remain in the emitted JavaScript.
  2. The call greet("TypeScript") passes text, so it satisfies the declared parameter type. The template literal combines Hello, the supplied name and an exclamation mark.
  3. console.log prints Hello, TypeScript! when Node runs dist/hello.js. The compiler checks the source first; Node executes the JavaScript that it emits.
  4. Change the call to greet(42) and run the noEmit check to see a diagnostic. Restore the string before compiling and running the example.

Best practices

  • Use a supported Node.js LTS release and install TypeScript locally in each project.
  • Review the generated tsconfig.json before relying on its version-dependent defaults.
  • Run a noEmit type check separately when you need diagnostics without generated files.
  • Run and test the emitted JavaScript in the environment for which you built it.

Runtime & versions

Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

TypeScript Handbook

Specifications & further reading