I have spent an hour now searching for a solution to a new error after needing top-level await. Everything else I have tried so far did not solve the error such as adding "type": "module"
to the package.json file.
The message of the error is Cannot use import statement outside a module
when starting the service.
If I revert the change "module": "ESNext",
to "module": "commonjs",
, it works fine (except the await keywords have to be removed and somehow refactored to work without await).
In addition, I use ts-node-dev to run the service which can be seen in the package.json file.
- The new package I need is kafkajs.
- node version: v14.9.0
- TypeScript version: 4.0
package.json
{
"name": "",
"version": "1.0.0",
"description": "microservice",
"main": "src/index.ts",
"author": "",
"type": "module",
"license": "MIT",
"scripts": {
"dev": "NODE_ENV=development tsnd --respawn --files src/index.ts",
"prod": "NODE_ENV=production tsnd --respawn --transpile-only --files src/index.ts",
"test": "mocha --exit -r ts-node/register tests/**/*.spec.ts",
"eslint": "eslint src/**/*.ts"
},
tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"outDir": "dist",
"sourceMap": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"ts-node": {
"files": true,
"transpileOnly": true
},
"include": ["src/**/*.ts", "declariations/**.d.ts"],
"exclude": ["node_modules", ".vscode"]
}