On nodejs 21
npm i -D tsx
node --import tsx src.ts
This works perfectly transpiling source code on-the-fly. Including whatever es module expretions like
import {Something} 'mod'
export const what = 'ever'
Still trying to run a ts source in the vm withing the same env
import {runInContext, createContext} from 'vm'
runInContext(`
export const a = 'some'
`,
createContext({...}),
)
fails with
export export const a = 'some'
^^^^^^
SyntaxError: Unexpected token 'export'
even trying previously to compile manually ts source
import {runInContext, createContext} from 'vm'
import * as ts from 'typescript'
runInContext(
ts.transpileModule(`
export const a = 'some'
`, {...}).outputText,
createContext({...}),
);
with whatever target
, module
, moduleResolution
till fails with different yet all the way syntax errors similar to
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: exports is not defined
So please what would be the proper way to make the vm to reuse the same importer defined with --import rsx
for the host context?
Or at least to compile properly the source manually so that it handled correctly within the vm env?
Thank you.