This is a starter repo for TypeScript, containing everything needed to write, test, and build TypeScript for a Node.js runtime. It's intended as a simple command-line playground for TypeScript.
npm install
- To run tests:
npm run test
- To build and bundle:
npm run build
- To run the bundled output:
node out.js
The npm run build
script does two things:
- Run
tsc
, transpiling all non-excluded files into JS and outputting them into thebuild
folder - Run
esbuild
usingbuild/index.js
as the entry point andnode
as the target platform, bundling the result into./out.js
.
I prefer using ES modules over CommonJS modules:
import addNumbers from "./addNumbers";
However, if I do this, any script using such an import
statement will fail after transpilation. Quoting from the TypeScript docs on module resolution and extensionless relative paths
Extensionless relative paths are not supported in import paths in Node.js...
There are no plans to support extensionless relative paths in Node.js. So, in order for the import
statement to work as intended after transpilation, I'd need to write:
import addNumbers from "./addNumbers.js";
This makes very little sense to me. It's a huge leak in the TypeScript abstraction. I prefer to labor under the illusion that I am not actually writing JavaScript, and I prefer not to think at all about how TypeScript gets transformed into something that will execute in the Node runtime.
To work around this, I use TypeScript with the bundler
module resolution configuration, which is the only module resolution configuration that supports extensionless relative paths. Naturally, this produces a tsc
build that will not run on Node without bundling. So, I picked the fastest bundler that would solve this problem.
tl;dr: the sole reason for using esbuild
in this repo is to allow me to use ESM instead of CJS.
- Add TypeScript
- Add
jest
- Add
esbuild
- Add
eslint
- Add
prettier