Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 581 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nodejs, TypeScript, ts-node-dev & top-level await

#1
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"]
}
```
Reply

#2
**TL;DR**: Don't use ECMAScript modules with ts-node or ts-node-dev (yet); just refactor the top-level await out

Today I tumbled down the same rabbit hole, starting with the innocuous error:

```
Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
```

As a result, I felt inclined to edit my `tsconfig.json` and set `module` to `esnext`, which in turn forced me to set `moduleResolution` to `node` and finally add `type: module` to my `package.json`. What I failed to realize (and IMO they shouldn't suggest this in the error message-- you can just simply refactor the top-level await out), that this switches the module resolution strategy of NodeJS from CommonJS to ESM. This is actually a big deal for many reasons:

* [ESM support is still experimental in ts-node](

[To see links please register here]

)
* [Mixing CJS and ESM modules may lead to problems](

[To see links please register here]

)
* [There are open issues with ESM support in NodeJS itself](

[To see links please register here]

)

I feel that at this time (december 2020) the NodeJS ecosystem is still in transition from the classic CommonJS modules to the new ECMAScript modules. As a result other tech like ts-node or ts-node-dev are transitioning as well and support can be flaky. My advice is to stick with CommonJS until the dust has settled and these things "just work" out of the box.
Reply

#3
## Update Node v18.12.0

You can now also use the built-in `--watch` flag from Node.

Assuming your entry file is `src/index.ts`:

```sh
node --watch --loader ts-node/esm --experimental-specifier-resolution node src
```

## Original Answer

I know, the question is quite old already, but for people passing by this issue like I did earlier today:

I just managed to get it to work. Not with `ts-node-dev` directly, but with the combination of `nodemon` and `ts-node`.

You need to install `nodemon` and `ts-node` and then, assuming your entry file is `src/index.ts`, you can use this command:
```
nodemon --ext ts --loader ts-node/esm --experimental-specifier-resolution node src
```

(Tested with Node v16.13.0)

This was the key post for me to figure it out:

[To see links please register here]

Reply

#4
Know that there is already an accepted answer to this question, but I think that is outdated right now (as of Dec 2022). Still `ts-node-esm` support is marked experimental. You can easily set up the flow you want though.

[To see links please register here]

for the relevant section in ts-node and a code base example if needed:

[To see links please register here]

.

```
// tsconfig.json

{

"extends": "ts-node/node16/tsconfig.json",
"ts-node": {
"esm": true,
....
"compilerOptions": {
"module": "ESNext",
"target": "ES2017"
}
},
"compilerOptions": {
// Just to make the lint shut up!
"module": "ESNext",
"target": "ES2017"
}
}
```

Don't forget `"type": "module"` in `package.json`

Please consider changing the accepted answer/updating it.
Reply

#5
It should be possible to use ts-node and have top level await.

Could you try `ts-node --esm {file}` as your command.

I'm currently using top level await and ts-node on [this code here][1].

I've also set my tsconfig target as `es2022` and my package.json type to `module`.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through