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

Query builder: TypeScript with ESM not working properly #455

Open
cojomchi opened this issue Sep 30, 2022 · 6 comments
Open

Query builder: TypeScript with ESM not working properly #455

cojomchi opened this issue Sep 30, 2022 · 6 comments
Assignees
Labels
bug upstream:node upstream:typescript Issue produced by changes to TypeScript

Comments

@cojomchi
Copy link

I am using Node 16 + ESM + Strictest for my tsconfig.json

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 16 + ESM + Strictest",
  "compilerOptions": {
    "lib": [
      "es2021"
    ],
    "module": "es2022",
    "target": "es2021",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "allowUnusedLabels": false,
    "allowUnreachableCode": false,
    "exactOptionalPropertyTypes": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noPropertyAccessFromIndexSignature": true,
    "noUncheckedIndexedAccess": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "importsNotUsedAsValues": "error",
    "checkJs": true
  }
}

Please note that I am not using NodeNext for "module" because some libraries are not supported properly (I am using Fastify).
And I also have set "type": "module" on package.json

In this situation, --target ts option does not work with following error:

 Cannot find module 'src/querybuilder/syntax/external' imported from src/querybuilder/index.ts

Which is because the import statement does not include .js extension in index.ts

export * from "./syntax/external";

To make this work, I have to modify all import statement to include .js extension.

Another workaround is using --target cjs and rename index.mjs to index.js so that it can be properly imported from ts file.

@BD103
Copy link

BD103 commented Mar 15, 2023

I found this issue while using Nuxt 3's build command with Vite. The workaround that I used was adding --target esm to the generation arguments. I need to do further testing to see how this affects the program.

I would be willing to create a reproducible example program, if requested.

Edit:

Turns out that building with --target ts works exclusively when using the Nuxt dev server (nuxt dev), while building with --target esm works exclusively when building for production (nuxt build).

BD103 added a commit to Phoenix-Wing/Reservoir that referenced this issue Mar 15, 2023
edgedb/edgedb-js#455

Essentially @edgedb/generate doesn't care about import file extensions, but Node does.
@scotttrinh
Copy link
Collaborator

This issue is becoming more prominent with the advent of Node v20, so I agree we should reconfigure the module work to get compatibility with all of the current LTS versions of Node plus Deno.

@scotttrinh scotttrinh added bug upstream:typescript Issue produced by changes to TypeScript upstream:node labels May 3, 2023
@emmanuelbuah
Copy link

emmanuelbuah commented May 25, 2023

Any idea when this might be addressed? I'm kinda blocked on my project unless I keep editing the import extensions on every code generation which is extremely time consuming.

@scotttrinh
Copy link
Collaborator

@emmanuelbuah

Any idea when this might be addressed? I'm kinda blocked on my project unless I keep editing the import extensions on every code generation which is extremely time consuming.

Nothing to share as far as timeline is concerned here. Does the target-based-on-env workaround work for your use case? If not, can you share a little about your setup (build tooling, framework, etc) that makes it unique?

@emmanuelbuah
Copy link

@scotttrinh I found a workaround for the problem by using --target ts. However, I had to modify my tsconfig file and set the moduleResolution option from "nodenext" (esm) to "node". This works for me because I have a mono repo where I can configure the resolution for each package separately. But I understand that this might not be feasible for some situations where you don't have that flexibility.

For your information, I'm using Nx to manage and build my mono repo, and node + react + graphql + flutter as my frameworks. So nothing too unusual (I think). Oh, its also a refactor from prisma to edgedb.

@KaelWD
Copy link

KaelWD commented Jul 10, 2024

I'm using tsup to build, the generated code is ok because it's bundled but if I want to keep edgedb itself separate I have to add exports to its package.json:

"exports": {
  ".": "./dist/index.node.js",
  "./dist/*": "./dist/*.js"
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug upstream:node upstream:typescript Issue produced by changes to TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants