|
| 1 | +# setup nodejs with typescript |
| 2 | + |
| 3 | +Initialize a Project |
| 4 | + |
| 5 | +``` |
| 6 | +npm init |
| 7 | +``` |
| 8 | + |
| 9 | +First, you need TypeScript installed absolutely.You can decide to have it installed globally on your machine, or locally to your project. |
| 10 | + |
| 11 | +tsc is the TypeScript compiler and has a command line interface with plenty of available options. |
| 12 | + |
| 13 | +To install TypeScript globally, run: |
| 14 | + |
| 15 | +``` |
| 16 | +npm i typescript -g |
| 17 | +tsc --init |
| 18 | +``` |
| 19 | + |
| 20 | +And to install locally, run: |
| 21 | + |
| 22 | +If you’re using a version of TypeScript installed locally, you’ll instead want to ensure that you’re running the local version by simply using npx. |
| 23 | + |
| 24 | +``` |
| 25 | +npm install typescript --save-dev |
| 26 | +npx tsc --init |
| 27 | +``` |
| 28 | + |
| 29 | +After running tsc with the ‘--init’ flag, a ‘tsconfig.json’ file will be added to your project folder with a few sensible defaults and an extensive list of commented-out possible configurations. |
| 30 | + |
| 31 | +For our project the file ‘tsconfig.json’ are as follows |
| 32 | + |
| 33 | +``` |
| 34 | +{ |
| 35 | + "compilerOptions": { |
| 36 | + "declaration": true, |
| 37 | + "experimentalDecorators": true, |
| 38 | + "emitDecoratorMetadata": true, |
| 39 | + "module": "commonjs", |
| 40 | + "esModuleInterop": true, |
| 41 | + "target": "es6", |
| 42 | + "listFiles": true, |
| 43 | + "noImplicitAny": false, |
| 44 | + "moduleResolution": "node", |
| 45 | + "sourceMap": true, |
| 46 | + "noEmitHelpers": false, |
| 47 | + "alwaysStrict": true, |
| 48 | + "suppressImplicitAnyIndexErrors": false, |
| 49 | + "forceConsistentCasingInFileNames": true, |
| 50 | + "resolveJsonModule": true, |
| 51 | + "outDir": "dist/", |
| 52 | + "diagnostics": true, |
| 53 | + "baseUrl": ".", |
| 54 | + "paths": { |
| 55 | + "*": ["node_modules/*"] |
| 56 | + }, |
| 57 | + "typeRoots": ["@types", "node_modules/@types"] |
| 58 | + }, |
| 59 | + "include": ["src/**/*"], |
| 60 | + "exclude": ["node_modules"] |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +With the tsconfig.json in place, we can start coding our app in TypeScript. |
| 65 | + |
| 66 | +we will add following scripts in `package.json` |
| 67 | + |
| 68 | +``` |
| 69 | +"build": "tsc", |
| 70 | +"start": "tsc && ts-node --files ./src/index.ts" |
| 71 | +``` |
| 72 | + |
| 73 | +and then we can use `npm run build` to transpile typescript files. With this, the JavaScript files that the TypeScript compiler outputs will be in a dist folder and sourceMaps will be generated. |
| 74 | + |
| 75 | +similarly we can use `npm run start` to run the `src/index.ts` file, where we will put our server initialization code soon. |
| 76 | + |
| 77 | +# Configuring ESLint |
| 78 | + |
| 79 | +[docs](https://eslint.org/docs/user-guide/getting-starteds) |
| 80 | + |
| 81 | +ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs. |
0 commit comments