    but **works with TypeScript
files**.
1. You have a running node.js process, and you want to import .ts file from
it
2. BUT you realize you can't do import './myfile.ts',
require('./myfile.ts') or await import('./myfile.ts').
3. And you DON'T want an additional compilation step before the process
starts
This is where import-single-ts comes in. It allows you doawait importSingleTs('./myfile.ts') with _no extra steps needed_.
A common example would be defining things like configuration or setup in a
type-safe (_ts_) environment. Think vite.config.ts, webpack.config.ts, etc.
1. Make sure you've installed the esbuild (_it's a peer dependency_)
2. ``ts`
import { importSingleTs } from 'import-single-ts';
// or for cjs: const { importSingleTs } = require('import-single-ts');
// ...
await importSingleTs('./some.ts'); // place where you need it
It has the same API as the native dynamic import â import()\*.
\* With some optional extras (_see below_).
- đ Drop-in replacement for import() â no learning curve, you can just
replace the dynamic await import('./some.js') calls withawait importSingleTs('./some.ts')
and it will just work as expected.esbuild
- ⥠Fast â uses internally and learns from the best (_vite's &.ts
esbuild plugins' source code_)
- đ Only compiles the minimum â The file being loaded may havenode
multiple imports, which can, in turn, have more imports at any depth. Any
imported files, which can run by itself, are not compiled. Instead,esbuild
they are loaded from their original path which means they are kept in the
internal node module cache and there won't be duplicate module executions if
they are imported again.
- đ No dependencies + 1 peer dependency of esbuild
- đ§Šī¸ Customizable import resolution â it exposes
options used in ,`
so you can provide things like
custom conditions
for
conditional exports,
aliases, etc. Simply pass in a second argument like so:
ts`
await importSingleTs('./some.ts', { conditions: ['mycompany-dev'], alias: { a: "b" }, ... })
bun
- đģī¸ Node.js REPL is supported as well
- âī¸ Not intended for âbun
TypeScript is supported out of the box in , no need to use this package.Windows
- âī¸ Not intended for â it's not tested on Windows and I won't be
able to dedicate extra time to debug problems there.
If this makes your work easier,
consider becoming a sponsor.
- I wanted to load up the exports from .ts file and use it as a type-safevite
config. There didn't seem to be an easy way to do that.
- I looked into
'svite.config.ts
internal logic that deals with loading up file. Beforeesbuild
settling on using I wasn't sure if there was a better way to doesbuild
compile on-the-fly. When I saw vite's approach I was relieved that looks like
the only way, and I've also adapted pieces of their config handling code
- I also researched
node-resolve plugin.esbuild
This was helpful to quickly glance plugin system. Sadly this reliesresolve
on the package whichpackage.json
doesn't support "exports" at all.require.extensions
- I noticed that
node is deprecated.
They actually recommend compiling ahead of time which is what this package
does.
- ts-import â it internally uses
tsc, so loading up a file is _slow_. Another problem of tsc is it can
run only for a single project, so if you are in a monorepo environment, and
you depend on typescript files from other projects it won't work as expected.
- It's build as cjs but can be used in both CJS and ESM environments.mjs
That's because in can't use stack trace to figure out the directoryimportSingleTs` was called from:
where
https://github.com/nodejs/node/issues/46992