Photo by Blake Connally on Unsplash
If you’re a developer, you know that starting a new project can be a daunting task. Setting up your development environment, installing the necessary packages, configuring the build tools, and more can take a lot of time and effort. Fortunately, TypeScript makes it easy to create a new Node project without all the hassle. In this post, we’ll walk you through the steps to generate a quick Node project with TypeScript.
Here are the steps you need to follow:
-
Install Node.js and npm if you haven’t done it yet.
-
Open a terminal window and navigate to the folder where you want to create your project.
-
Run the following command to create a new folder for your project:
mkdir my-project && cd my-project
- Initialize npm for your project using the following command:
npm init -y
- Install the necessary dependencies for a TypeScript project:
npm install --save-dev typescript ts-node @types/node
- Create a TypeScript configuration file named
tsconfig.json
in your project’s root directory with the following content:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
- Create a folder named
src
in the root directory of your project, and create a TypeScript file namedindex.ts
inside it:
mkdir src && touch src/index.ts // linux
mkdir src ;; New-Item -Type File src/index.ts // windows
- Open the
index.ts
file in your favorite code editor and write some TypeScript code, for example:
const message: string = 'Hello, TypeScript!';
console.log(message);
- In your
package.json
file, add a new script to compile and run your TypeScript code:
{
"scripts": {
"start": "ts-node src/index.ts",
"build": "tsc"
}
}
- Finally, run the following command to compile and run your TypeScript code:
npm run start
This will compile your TypeScript code into JavaScript and run it using ts-node
. You can also run the build
script to compile your code and generate JavaScript files in the dist
folder:
npm run build
Thanks and have a great day ahead!
Don’t forget to give this post a heart if you found it useful. It helps others discover helpful posts like this one.