esm-loader-typescript is a Node.js ESModule loader designed for on-the-fly transpilation of TypeScript (`.ts`) files within an ESModule environment. It leverages Node.js's experimental loader API, facilitating direct import of TypeScript files without a pre-compilation step. Currently at version 1.0.6, the package is primarily suited for development and testing workflows, with an explicit warning against production use due to the inherent instability and potential for breaking changes in Node.js's experimental loader API. It supports two main modes of operation: standalone activation via `NODE_OPTIONS` (using `--loader` for Node.js < 20.7 or `--import` with a registration file for Node.js >= 20.7), and a chainable mode using `node-esm-loader` and a `.loaderrc.js` configuration. Its key differentiators include simple integration into existing ESM projects, the ability to specify a custom `tsconfig.json` or enable debug logging via options, and its role as a building block for more complex loader chains.
npm install esm-loader-typescriptVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to install and configure `esm-loader-typescript` using `node-esm-loader` to transpile and run a TypeScript file directly with Node.js.
Monitor Node.js release notes for loader API changes and update 'esm-loader-typescript' accordingly. Avoid using in production environments where stability is critical.
For Node.js < 20.7, use 'NODE_OPTIONS="--loader esm-loader-typescript"'. For Node.js >= 20.7, create a 'register.js' file with 'import { register } from "node:module"; register("esm-loader-typescript", import.meta.url);' and use 'NODE_OPTIONS="--import ./register.js"'. Alternatively, use 'node-esm-loader' which abstracts these differences.Restrict usage to development and testing environments. For production, pre-compile TypeScript or use established solutions like ts-node with appropriate production configurations and stability guarantees.
Ensure `NODE_OPTIONS` is correctly set (e.g., `--loader esm-loader-typescript` for Node < 20.7, or `--import ./register.js` for Node >= 20.7, or `--import node-esm-loader/register` with a `.loaderrc.js` config).
Update your command to use `NODE_OPTIONS="--import ./register.js"` where `register.js` is a file containing `import { register } from 'node:module'; register('esm-loader-typescript', import.meta.url);`.Ensure `tsconfig.json` exists in the project root or provide the correct path using `{ loader: 'esm-loader-typescript', options: { config: 'path/to/your-tsconfig.json' } }` in your `.loaderrc.js`.