Giter VIP home page Giter VIP logo

node-ts-setup's Introduction

Installation

Create file for entrypoint of project server.ts

Implement some code

console.log('test')

Run server.ts

node server.ts

It will be fine until you implement ts code like this

const test: number = 0

Global installation

yarn global add tsc typescript ts-node ts-node-dev

You can run ts file like it

tsc server.ts

It will be converted to a file of Javascript by generate new file (in this case is server.js)

Then you can run directly on .js file as node server.js.

What is ts-node and ts-node-dev?

ts-node is used to compile .ts with tsc and run .js with node.

It works like ts-node server.ts equals to tsc && node server.js

And ts-node-dev, it works like nodemon for TypeScript.

TypeScript Configuration

You can config ts by init commands

tsc --init

see tsconfig.fson will be display, uncomment rootDir to specify the root folder within your source files.

After that, you can set up the root folder for compiler by change "./" to be any directory you need.

In this case, we move server.ts into src, and set up rootDir to be "./src".

Anything outside src would not be compiled.

A folder for contain .js after compile from .ts

We define a new row on tsconfig.fson if below rootDir will be better.

Named outDir for contain .js file from compile .ts, in this case, call it to dist.

Then, see allowJs and uncomment it, using to allow .js files to be a part of your program.

And the project will be allowed to compile .js files as well as .ts file.

Add Libraries

Generate package.json for the project as normal.

npm init --y

Add .gitignore file for control unnecessary files or folders to the version control.

touch .gitignore

inside the file, type node_modules to ingore the folder pushed to Git.

Then, add Express

yarn add express

Add

yarn add @types/express -D

or

yarn add @types/express --dev

Add Alias on package.json

In script add new line to be

 "start": "ts-node src/server.ts"

Using when you need start server, you just type command yarn start instead of ts-node src/server.ts.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.