Build a node project for TypeScript environment

In this article, we use a different way to create the project.
Here we create a folder as the project directory in advance
Insert image description here
and then open the project terminal and enter

npm init

Then in the new dialog box that pops up, just write a random name and press Enter. Enter yes in the last one and press Enter.
Insert image description here
This way we have a basic node project structure.
Insert image description here
Then we introduce the TS dependency globally and
enter the terminal.

npm install -g typescript

Then open the command window as an administrator and
Insert image description here
execute the terminal

tsc --init

Insert image description here
In this way, we have a ts configuration file
Insert image description here
and there are two pieces of information here: outDir, the output directory information rootDir, and the source code path.
Insert image description here
You can refer to my configuration. Of course, you can also modify it according to your actual situation. Anyway, this is all defined by yourself.
Insert image description here
Then we We need to create these two directories corresponding to our own configuration.
Insert image description here
Then we create an index.ts in the src directory. The content is as shown below.
Insert image description here
Then we add a command to the scripts of package.json.

"start": "tsc && node out/index.js"

Insert image description here
Then we run the terminal

npm start

Here our content is output in the terminal, and here ts also helps us parse it using js syntax.
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/133463503