edgedb / edgedb-js

The official TypeScript/JS client library and query builder for EdgeDB
https://edgedb.com
Apache License 2.0
513 stars 65 forks source link

Remix/Vite/EdgeDB: When using edgeql-js with tsx, imported modules are not resolved correctly. #1022

Open benrbowers opened 5 months ago

benrbowers commented 5 months ago

Code

import e from '../dbschema/edgeql-js';

const insertCitizens = e.params({ citizens: e.json }, (params) => {
    return e.for(e.json_array_unpack(params.citizens), (citizen) => {
        return e.insert(e.Citizen, {
            name: e.cast(e.str, citizen.name),
            identity: e.insert(e.ext.auth.LocalIdentity, {
                issuer: e.cast(e.str, citizen.identity.issuer),
                subject: e.cast(e.str, citizen.identity.subject),
                modified_at: e.cast(e.datetime, new Date()),
            }),
        });
    });
});

>> yarn tsx seed/seed-citizens.ts

Error or desired behavior

"Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/ben/democracy-app/node_modules/edgedb/dist/reflection/index' imported from /home/ben/democracy-app/dbschema/edgeql-js/set.ts Did you mean to import "edgedb/dist/reflection/index.js"?"

Manually changing all occurrences of import from "edgedb/dist/reflection/index" and import from "edgedb/dist/primitives/buffer" to import from "edgedb/dist/reflection/index.js" and import from "edgedb/dist/primitives/buffer.js" fixes the issue.

Versions

scotttrinh commented 5 months ago

What does your tsconfig.json look like for this project? You might need to set "moduleResolution": "bundler" (see https://www.typescriptlang.org/docs/handbook/modules/theory.html#module-resolution-for-bundlers-typescript-runtimes-and-nodejs-loaders for more details).

We have an open issue to rehaul the TS output to take into account all of the new module resolution modes supported by both TypeScript, alternative runtimes (like Deno and Bun), and bundlers, but in the meantime we can figure out what the right incantation for your own particular setup should be.

benrbowers commented 5 months ago

Default tsconfig from create-remix@latest:

{
    "include": [
        "**/*.ts",
        "**/*.tsx",
        "**/.server/**/*.ts",
        "**/.server/**/*.tsx",
        "**/.client/**/*.ts",
        "**/.client/**/*.tsx"
    ],
    "compilerOptions": {
        "lib": ["DOM", "DOM.Iterable", "ES2022"],
        "types": ["@remix-run/node", "vite/client"],
        "isolatedModules": true,
        "esModuleInterop": true,
        "jsx": "react-jsx",
        "module": "ESNext",
        "moduleResolution": "Bundler",
        "resolveJsonModule": true,
        "target": "ES2022",
        "strict": true,
        "allowJs": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "baseUrl": ".",
        "paths": {
            "~/*": ["./app/*"]
        },

        // Vite takes care of building everything, not tsc.
        "noEmit": true
    }
}
benrbowers commented 3 months ago

After moving from yarn to pnpm, this now works with no issues:

>> pnpm exec tsx seed/seed-citizens

pnpm: 9.6.0 edgedb version: 5.6+51fd5fe EdgeDB CLI: 5.2.2+270dbfa edgedb-js: 1.5.7 node: 20.12.2

scotttrinh commented 3 months ago

😅

I still plan on doing a "target" update here to allow building node16 style module imports, but we do not have any progress to report on that yet.