How to set up a Node.js Typescript project well
2022-11-19
This post covers how to set up a project with Node.js and Typescript using eslint and vscode devcontainers using Docker.
This project makes use of eslint with airbnb styling and typescript support.
All linting errors can be automatically fixed (usually) by running npm run fix
before you commit your changes.
Getting it running locally for development
Devcontainer + vscode
If you have Visual Studio Code
and Docker
installed then you can open this project using the Remote - Containers
extension (ms-vscode-remote.remote-containers). Click the button at the bottom left of vscode, then 'Reopen in container'. You can now run the application using the vscode debugger.
Standard local install
Running npm run dev
will build the development version in the dist
folder and enable hot reloading. It will also open the application in a new browser tab automatically.
Running npm run build
will create the production assets in the dist
folder.
Devcontainer
In the .devcontainer
folder you'll need three files.
base.Dockerfile
# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 18, 16, 14, 18-bullseye, 16-bullseye, 14-bullseye, 18-buster, 16-buster, 14-buster
ARG VARIANT=16-bullseye
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}
# Install tslint, typescript. eslint is installed by javascript image
ARG NODE_MODULES="tslint-to-eslint-config typescript"
COPY library-scripts/meta.env /usr/local/etc/vscode-dev-containers
RUN su node -c "umask 0002 && npm install -g ${NODE_MODULES}" \
&& npm cache clean --force > /dev/null 2>&1
# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>
# [Optional] Uncomment if you want to install an additional version of node using nvm
# ARG EXTRA_NODE_VERSION=10
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"
devcontainer.json
Dockerfile
One important thing to note in this file is the installation of inotify-tools
to the underlying Debian OS, as this is required for nodemon to be able to poll and do cold reloading.
# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 18, 16, 14, 18-bullseye, 16-bullseye, 14-bullseye, 18-buster, 16-buster, 14-buster
ARG VARIANT=16-bullseye
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT}
# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends inotify-tools
# [Optional] Uncomment if you want to install an additional version of node using nvm
# ARG EXTRA_NODE_VERSION=10
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"
# [Optional] Uncomment if you want to install more global node packages
# RUN su node -c "npm install -g <your-package-list -here>"
Visual Studio Code Debugging
Within the .vscode
folder you'll need to create this launch.json
file:
Eslint
Create the following files:
.eslintignore
Assuming you're using git, this will be the same as your .gitignore
file.
.eslintrc
TS support, plus cold reloading
The structure for this application is to have a src
folder that contains all .ts
files. The files will be compiled to a dist
folder for deploying to a server.
Create the following files: