Node.js is a popular JavaScript runtime environment that is used to build scalable and performant server-side applications. TypeScript is a superset of JavaScript that adds type safety and other features to the language.
In this article, we will show you how to get started with Node.js and TypeScript in just two minutes.
Prerequisites
You must have Node.js and VS Code installed.
Open terminal and start executing the below commands:
mkdir my-app && cd my-app
npm init -y
npm install -D typescript
npx tsc --init
mkdir src && cd src
echo "console.log('Hello World!');" >> index.ts
cd ..
code .
Note: Last command will open the project folder in vscode editor 😉
Inside VSCode, open tsconfig.json and replace the content with below:
{
"compilerOptions": {
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "src", /* Specify the root folder within your source files. */
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
"declarationMap": true, /* Create sourcemaps for d.ts files. */
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": [
"src/**/*"
]
}
Next, open package.json and add below two script commands to scripts tag:
"build": "npx tsc",
"start": "npm run build && node dist/index.js",
Your package.json should look something like:
{
"name": "my-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "npx tsc",
"start": "npm run build && node dist/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^5.2.2"
},
}
That's it! Now open terminal using shortcut key:
ctrl + shift + ` (backtick key)
Run the below command:
> npm run start
TADA, you should see the output in the console as:
Hello World!
Top comments (0)