Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ERROR] Cannot use import statement outside module #310

Open
angelhdzmultimedia opened this issue May 19, 2022 · 2 comments
Open

[ERROR] Cannot use import statement outside module #310

angelhdzmultimedia opened this issue May 19, 2022 · 2 comments

Comments

@angelhdzmultimedia
Copy link

angelhdzmultimedia commented May 19, 2022

Issue description

I get Cannot use import statement outside module error. I'm familiar with this error, so I tried to solve it by adding type: "module" in package.json. It didn't solve it. So I researched and added target: "esnext" and module: "esnext" in tsconfig.json, but then I lost the ability to use Top Level Await. So my only option is Not Using Top Level Await and setting module: "commonjs" in tsconfig.json.

Context

A simple NodeJS/Express/TypeScript RESTful API.

OS version (is it docker or host?)
Windows 10 Home 10.0.19043 Build 19043

ts-node-dev version
1.1.8

Did you try to run with ts-node?
No

Did you try to run with --files option enabled?
No

Did you try to run with --debug option enabled?
No

Do you have a repro example (git repo) with simple steps to reproduce your problem? Not at this time.

package.json:

{
  "type": "module",
  "name": "test_prisma",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "dev": "ts-node-dev --respawn src/app.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.13",
    "@types/node": "^17.0.34",
    "@types/source-map": "^0.5.7",
    "ts-node-dev": "^1.1.8",
    "typescript": "^4.6.4"
  },
  "dependencies": {
    "@typegoose/typegoose": "^9.8.1",
    "express": "^4.18.1",
    "mongoose": "^6.3.3"
  }
}

Related Issue

#265

@StephanBijzitter
Copy link

I'm having the same issue, everything (at least that I know of) works, except top-level await.

tsconfig.ts-node-dev.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}

ts-node-dev --project tsconfig.ts-node-dev.json ...etc

It's okay, but not ideal

@angelhdzmultimedia
Copy link
Author

angelhdzmultimedia commented Aug 11, 2023

I'm having the same issue, everything (at least that I know of) works, except top-level await.

tsconfig.ts-node-dev.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}

ts-node-dev --project tsconfig.ts-node-dev.json ...etc

It's okay, but not ideal

I've been using node --watch --experimental-specifier-resolution=node --loader ts-node/esm main.ts and EsNext in both module and target in tsconfig.json. Maybe it helps you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants