SyntaxError: Cannot use import statement outside a module
// 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
Try to change
"module": "ESNext",
"moduleResolution": "nodenext",
Insted ts-node
ts-node-esm
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?