You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Both give the below output
/home/juha/workspace/testent/simple-typescript-project-example/[stdin].ts:1
import { parse } from 'path';
^^^^^^
SyntaxError: Cannot use import statement outside a module
This on the other hand works and it's exactly the same code, just without the eval. npx ts-node-esm ./src/testCode.ts
If I change package.json "type" to "commonjs" and tsconfig.json compilerOptions.module to "CommonJS" then the both npx ts-node-esm -e .... and MYCODE=$(cat "./src/testCode.ts") && echo $MYCODE | npx ts-node-esm work.
Does anyone know what is the reason for the different behavior between eval and "normal" execution with ts-node-esm when package.json "type" is "module" and tsconfig.json compilerOptions.module is "ESNEXT"?
How could I make the MYCODE=$(cat "./src/testCode.ts") && echo $MYCODE | npx ts-node-esm work when package.json "type" is "module" and tsconfig.json compilerOptions.module is "ESNEXT" (Or not "Commonjs" or "NodeNext")?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I wondering why ts-node-esm gives an "Cannot use import statement outside a module" error.
Expected Behavior
MYCODE=$(cat "./src/testCode.ts") && echo $MYCODE | npx ts-node-esm
Prints:
/first/second
npx ts-node-esm -e 'import { parse } from "path";const output: string = parse("/first/second/file.ts").dir; console.log(output);'
Prints:
/first/second
Actual Behavior
Minimal reproduction
Node version
Node: v16.17.0
Ways which work
This on the other hand works and it's exactly the same code, just without the eval.
npx ts-node-esm ./src/testCode.ts
If I change package.json "type" to "commonjs" and tsconfig.json compilerOptions.module to "CommonJS" then the both
npx ts-node-esm -e ....
andMYCODE=$(cat "./src/testCode.ts") && echo $MYCODE | npx ts-node-esm
work.Does anyone know what is the reason for the different behavior between eval and "normal" execution with ts-node-esm when package.json "type" is "module" and tsconfig.json compilerOptions.module is "ESNEXT"?
How could I make the
MYCODE=$(cat "./src/testCode.ts") && echo $MYCODE | npx ts-node-esm
work when package.json "type" is "module" and tsconfig.json compilerOptions.module is "ESNEXT" (Or not "Commonjs" or "NodeNext")?Beta Was this translation helpful? Give feedback.
All reactions