This is a generic template for node + typescript projects. It bootstraps a template project with some popular packages and npm script commands pre-configured.
The entry point of the app is src/index.ts
create a new project in a new folder
npx create-node-typescript-app <folder>
or inside the project directory
npm init node-typescript-app
or npx create-node-typescript-app .
or install this module globally
npm i -g create-node-typescript-app
and run
create-node-typescript-app <folder>
A .env
file should be created first. In this template, .env
file is used for creating environment variables in the local dev environment. This file is used for local dev only, it is NOT used in the deployed environment (e.g. test, staging, prod etc). How you want to build env variables in your CI/CD process is not in the scope of this template.
npm run dev
- start the app in development mode (it reads the local .env file)npm run test:dev
- run tests locally (reads local .env)npm run lint
- run eslintnpm run format
- run prettiernpm run build:dev
- build typescript with source maps and comments in code are keptnpm run mocha
- a helper npm script for running customised mocha command e.g. test a single filenpm run mocha -- file-name-or-pattern
Production commands do not read the .env
file. How you want to build environment variables in production environments is not in the scope of this template.
Do NOT use production commands in the local development environment. They might NOT work as expected because these commands may reply environment variables from actual environments.
npm start
- start applicationnpm build
- compile typescript with no source maps and comments are removed from ts filesnpm test
- run tests
NODE_PATH
used in scripts are for improving the readability of import
statements e.g. Relative paths like import someModule from '../../../utils/module'
can be written as import someModule from 'src/utils/module'
This project has been configured with three steps of code quality controls
eslint --cache --fix
is triggered before each commit. This command tries to fix linting errors when it is possible.
eslint
has been configured to also check and fix formatting errors detected by prettier
. (https://prettier.io/docs/en/integrating-with-linters.html)
npm run test:dev
is triggered before each push. Push will fail if tests fail or test coverage is below the threshold defined in ./.nycrc
.
npm run audit
is triggered before each push. Push will fail if there are vulnerabilities in dependencies. You should run npm audit fix
to fix the vulnerabilities and commit the changes before you push again.
Run VS Code in Windows Subsystem for Linux
Copy launch.json and tasks.json from the .vscode folder to your project's .vscode
folder.
These configurations have only been tested with projects opened in Windows WSL mode.
Start (NodeTS WSL)
- starts the app in debug modeTest All (NodeTS WSL)
- run tests in debug modeTest Current File (NodeTS WSL)
- run test on the current open/focused file e.g. ifsomeFile.test.ts
is the file in focus, and you pressed the "start debugging" button or Ctrl + F5, this command will runmocha someFile.test.ts
- ESLint:
.eslintrc.json
.eslintignore
- Mocha:
.mocharc.json
- Istanbul:
.nycrc
- Nodemon:
nodemon.json
- Typescript:
tsconfig.json
- Prettier:
.prettierrc