knaxuslearn · build · ship

Projects / DI container / Before you start

Before you start

You need Node.js 20 or newer. Check it:

node --version

Every listing and command in this project was run on Node 22 with TypeScript 7.0.2 on Linux. Make a folder and install the compiler and the Node type definitions:

mkdir tiny-di && cd tiny-di
npm init -y
npm install --save-dev typescript @types/node

Tell Node that our files are ES modules. Create package.json (replace the generated one):

{ "name": "tiny-di", "version": "0.1.0", "type": "module" }

Now tsconfig.json, which tells the compiler how strict to be and where to put its output:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "declaration": true,
    "outDir": "dist",
    "rootDir": "src",
    "types": ["node"],
    "skipLibCheck": true
  },
  "include": ["src"]
}

A few of these lines matter more than they look. strict turns on the checks we are relying on. NodeNext makes imports behave like they do in real Node, which means our imports end in .js even though the files are .ts. And "types": ["node"] is explicit on purpose: with TypeScript 7.0.2 the node:test import in Phase 4 failed to compile without it, while older versions found @types packages by themselves. (The compiler's types option documents this.) Put every source file in a src folder. You build with npx tsc, which writes JavaScript to dist, and run it with node dist/<name>.js.