Saturday, September 3, 2022
HomeWordPress DevelopmentOrganising TypeScript with NodeJS

Organising TypeScript with NodeJS


The opposite day after I was organising TypeScript with a Node mission, I believed that I might write a easy information that would assist you to doing this in a hopefully easy method.

To start out do the next:

First just remember to have TypeScript put in by working or putting in:

tsc -- model
npm set up -g typescript
Enter fullscreen mode

Exit fullscreen mode

Now we have to add a bundle.json file:

npm init -y
Enter fullscreen mode

Exit fullscreen mode

So as to add TypeScript run the next command:

yarn add -D typescript
Enter fullscreen mode

Exit fullscreen mode

Subsequent, we have to initialize our mission with TypeScript. The command beneath will generate a file referred to as tsconfig.json. Make certain to learn and perceive this file because it incorporates helpful info and is nicely documented.

npx tsc -- init
Enter fullscreen mode

Exit fullscreen mode

Now create a brand new file in your mission and guarantee that the file extension is .ts. I’ll create a file referred to as app.ts inside a src folder. Contained in the file create a console.log.

src/app.ts
console.log('Hiya World');
Enter fullscreen mode

Exit fullscreen mode

Now we have to configure in order that the TypeScript information is compiled into JavaScript information. Go to the file bundle.json and change the content material within the scripts block with the next which can execute our TypeScript information and construct them into JavaScript information.

"scripts": {
    "construct": "tsc"
}
Enter fullscreen mode

Exit fullscreen mode

Now we will construct our mission and after we do that, we are going to get a .js file which we will use to run our software.

yarn construct // to construct
node app.js // to run
Enter fullscreen mode

Exit fullscreen mode

However that is type of annoying to do it this manner after we can run our TypeScript information instantly by including a library who handles this. Run the next command:

yarn add -D ts-node
Enter fullscreen mode

Exit fullscreen mode

Now go into your bundle.json file and the the next line in scripts:

"scripts": {
  "begin": "ts-node src/app.ts",
  "construct": "tsc"
}
Enter fullscreen mode

Exit fullscreen mode

You possibly can delete the beforehand generated .js file and now instantly working your TypeScript information:

yarn begin
Enter fullscreen mode

Exit fullscreen mode

In case you’re growing and need the modifications to be up to date every time you save, then you’ll be able to add the next library which can restart the server every time you make a change:

yarn add -D ts-node-dev
Enter fullscreen mode

Exit fullscreen mode

Now head into the bundle.json file and replace the scripts.begin:

"scripts": {
  "begin": "ts-node-dev --respawn src/app.ts",
  "construct": "tsc"
}
Enter fullscreen mode

Exit fullscreen mode

That is it, you might have now arrange TypeScript with Node.

Any feedback, questions or discussions are all the time welcome!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments