Either use
npm install --global typescript
or
npm install --save-dev typescript
If you use the --global option, then the TypeScript compiler tsc will be available in all projects on the same machine. It will also be available as a terminal command, but it will not be added to your package.json file. Therefore, if you share your code with others, TypeScript will not be installed when another person gets your code and runs npm install.
If you use --save-dev, TypeScript will be added to package.json and installed in your project’s ./node_modules/.bin/tsc, and you can still use tsc inside the npm "scripts" section of package.json.
-
If you would like to run a TypeScript file directly from the command prompt, the easiest way is to use
ts-node:npm install --global ts-nodeAfter installingts-node, runts-node X.tswhereX.tsis the name of a script you want to run. -
If you want to compile typescript to javascript do
tsc x.tswhere x is the name of file to executed js file runnode x.js
- "tsc": "tsc",
usage - when typescript is not globally installed use this script to compile ts file
npm run tsc X.tsoryarn tsc X.ts - "ts-node": "ts-node"
usage - when ts-node is not globally installed use this script to execute ts filenpm run ts-node X.tsoryarn ts-node X.ts
- if tsc command is giving compilation error for files in node_modules execute tsc with --lib
- ts-node does not work for RXJS.
- setup launch.json to execute the main.ts in debug
{
"name": "Debug Typescript",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/src/main.ts",
"runtimeArgs": ["-r" , "ts-node/register" , "-r" , "tsconfig-paths/register"],
"console": "integratedTerminal",
"outFiles": [
"${workspaceFolder}/**/*.js",
"!**/node_modules/**"
],
"type": "node"
},
- since launch is configured to launch main.ts in debug , make sure the file you need to test is called through main.ts