jiti
Runtime typescript and ESM support for Node.js (CommonJS)
Features
- Stable typescript and esm syntax support
- Provide sync interface to replace require
- Super slim and zero dependency
- Syntax detect to avoid extra transform
- CommonJS cache integration
- Filesystem transpile cache + V8 compile cache
Usage
Programmatic
const jiti = require('jiti')(__filename)
jiti('./path/to/file.ts')
You can also pass options as second argument:
const jiti = require('jiti')(__filename, { debug: true })
CLI
jiti index.ts
Register require hook
node -r jiti/register index.ts
Alternatively, you can register jiti
as a require hook programmatically:
const jiti = require('jiti')()
const unregister = jiti.register()
Options
debug
- Type: Boolean
- Default:
false
- Environment Variable:
JITI_DEBUG
Enable debug to see which files are transpiled
cache
- Type: Boolean | String
- Default:
true
- Environment Vriable:
JITI_CACHE
Use transpile cache
If set to true
will use node_modules/.cache/jiti
(if exists) or {TMP_DIR}/node-jiti
transform
- Type: Function
- Default: Babel (lazy loaded)
Transform function. See src/babel for more details
Compared to Alternatives
+
Much more stable thanks to babel+
Less low level operations+
Typescript support-
Slower (without cache)
+
Smaller install size (~1M vs ~11M with same plugins)+
Configured out of the box+
Smart syntax detect to avoid unnecessary transpilation+
Does not ignores node_modules
. ESM everywhere yay!+
Embeddable
+
No native dependency+
More stable thanks to babel-
Slower+
Embeddable
ts-node
+
Support both esm and typescript/
No typechecking support / Faster+
Smart syntax detect to avoid unnecessary transpilation
Native ESM Support (MJS)
- It is not (yet) landed as a stable feature
- No typescript support
- Limitted to
.mjs
files with different executation context (no __filename
, require
, etc)
Bundlers (rollup
, webpack
, snowpack
, etc)
Meanwhile it would be much better making an optimized bundle to deploy to production or as npm package, using bundler setup and watching is frustrating during project development that's where jiti
(or similar tools like ts-node
) would be more convenient.
Note: However currently only babel transform is supported, configurable transform support is in the roadmap so using esbuild
or other solutions would be possible.
Development
- Clone Repo
- Run
yarn
- Run
yarn build
- Run
yarn dev
- Run
node ./test/jiti.js
Roadmap
License
MIT. Made with 💖