ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

SyntaxError: Cannot use import statement outside a module

Open Zswsown opened this issue 3 years ago • 2 comments

// tsconfig.json { "compilerOptions": { "target": "esnext", "module": "CommonJS", "types": ["node"], "strict": true, "jsx": "preserve", "importHelpers": true, "moduleResolution": "node", "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "sourceMap": true, "baseUrl": ".", "outDir": "./output", "declaration": true, // 是否生成声明文件 "declarationDir": "./dist", // 声明文件打包的位置 "lib": ["esnext", "dom", "dom.iterable", "scripthost"], "noImplicitThis": false }, "include": ["src/**/"] / 指定需要编译的目录范围 /, "exclude": ["node_modules"] / 忽略类型检查的目录 */ }

When I want to import files by ’await import‘ or 'require()' in the koa server service layer,it made a mistake

SyntaxError: Cannot use import statement outside a module

Zswsown avatar Nov 30 '22 02:11 Zswsown

Try to change

"module": "ESNext",
"moduleResolution": "nodenext",

Insted ts-node

ts-node-esm

yaroslavx avatar Dec 18 '22 09:12 yaroslavx

I am facing this problem as well. I want (okay, want is to strong of a word.... I feel like I must) to use CommonJs. So, how can I resolve this for cjs?

coltenkrauter avatar Jan 16 '25 16:01 coltenkrauter