Setting up Node.JS with Typescript, Nodemon, Eslint, Mocha and Istanbul

Daniel Gong
3 min readNov 11, 2019

--

Did you know TSLint is deprecated? Let’s waste a whole day to set up everything again, or not.

btw, what do you call a bee from new york?

I was starting another Node + Typescript project, and I was thinking why don’t I just create a Node + Typescript template and reuse it again and again, it should only take 10 minutes, I had done it before, many times…

What a waste of time… I should have focused on the product instead, and you should too.

Let’s use these magic commands to save you some time.

# method 1
# create a new project
npx create-node-typescript-app <directory>
# or method 2
# initialise a project in a folder
npm init node-typescript-app

and all of a sudden, you have these files

./
../
src/
tests/
dist/
tmp/
README.md
.env
.eslintignore
.eslintrc.json
.gitignore
.mocharc.json
.nycrc
nodemon.json
package-lock.json
package.json
tsconfig.json

and some ready-to-use scripts in package.json

Run src/index.ts locally with environment variables loaded from .env

> npm run dev

Run *.test.ts files in the tests folder with Mocha and Istanbul (nyc)

# run all tests
> npm run test:dev
# or test a single file
> npm run mocha -- tests/src/somefile.test.ts

Run ESLint manually

> npm run lint

and when you try to commit with some dodgy formats, ESLint will either try to fix your code or stop you from committing.

and when you try to push your code to git, your failed tests and low coverage rate will stop you from pushing the code.

Hurray! your code reviewers now have fewer reasons to slap you.

Wait! what about npm start, npm test and npm build? They are for your CI/CD, don’t use them on your local machine.

Are you one of those weirdos like me who develops on a Windows machine with WSL?

There are some bonus files for VS Code here.

  • Start (NodeTS WSL) - starts the app in debug mode
  • Test All (NodeTS WSL) - run tests in debug mode
  • Test Current File (NodeTS WSL) - run test on the current open/focused file e.g. if someFile.test.ts is the file in focus, and you pressed the "start debugging" button or Ctrl + F5, this command will run mocha someFile.test.ts

source code? It’s on GitHub and npm

Oh, and the answer to the first question at the top is USB.

--

--