template-of-node-js-library
v1.0.0
Published
A loader of TypeScript for Node.js.
Readme
Template of Node.js library
Load .ts, .mts, and .cts files in Node.js by registering a custom load hook.
This package wires into Node's module loader (node:module) and strips TypeScript
types at load time, so files can run directly without a build step.
What this project does
- Registers a Node.js module hook via
registerHooks(...). - Intercepts TypeScript files based on extension:
.cts→ loads as CommonJS.mts→ loads as ESM.ts→ auto-detects module type from nearestpackage.json
- Uses
stripTypeScriptTypes(...)fromnode:moduleto remove TypeScript-only syntax. - Passes non-TypeScript files through untouched.
How module type is chosen for .ts
For plain .ts files, the loader walks up directories from the file location:
- If it finds a
package.jsonwith"type": "module", the file is treated as ESM. - Otherwise, it is treated as CommonJS.
- If no
package.jsonis found, CommonJS is used.
Requirements
- Node.js
>=24.14.0 - npm
>=11.9.0
Installation
npm install template-of-node-js-libraryUsage
1. Register through Node's import flag (recommended)
node --import template-of-node-js-library ./path/to/file.tsYou can also keep it enabled globally for a session:
export NODE_OPTIONS="--import=template-of-node-js-library"
node ./path/to/file.ts2. Register programmatically
Importing the package runs its side-effect entrypoint and registers the hook:
import "template-of-node-js-library";Notes and limitations
- This loader strips types; it does not perform full TypeScript transpilation.
- Prefer erasable TypeScript syntax compatible with
stripTypeScriptTypes(...). - Keep using
tsc --noEmit(or equivalent) for type checking in CI.
