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

Cannot Import ES Module #641

Closed
archy-bold opened this issue Jul 5, 2023 · 3 comments · Fixed by #696
Closed

Cannot Import ES Module #641

archy-bold opened this issue Jul 5, 2023 · 3 comments · Fixed by #696
Labels
released type/bug Something is not working the way it should

Comments

@archy-bold
Copy link

archy-bold commented Jul 5, 2023

Description

I'm getting an error when importing nexus-prisma from an ES module project.

(node:219860) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/simon/Development/west-app/web/node_modules/nexus-prisma/dist-esm/entrypoints/main.js:1
import { inspect } from 'util';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1176:20)
    at Module._compile (node:internal/modules/cjs/loader:1218:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Object.require.extensions.<computed> [as .js] (/home/simon/Development/west-app/web/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Repro Steps/Link

Import from a "type": "module" TypeScript project.

import { Model } from "nexus-prisma";

A possible workaround is to force require the cjs entrypoint.

const require = createRequire(import.meta.url);
const nxp = require("nexus-prisma/dist-cjs/entrypoints/main");
const { Model } = nxp;
@archy-bold archy-bold added the type/bug Something is not working the way it should label Jul 5, 2023
p-kuen added a commit to p-kuen/nexus-prisma that referenced this issue Jul 8, 2023
@rostislav-simonik
Copy link
Collaborator

rostislav-simonik commented Jul 30, 2023

@archy-bold @p-kuen I think there are more things to do to fully support esm modules. I believe nexus-prisma as esm never worked. For the following reasons

  • all relative or folder imports are still in commonjs syntax (esm requires to have index.js or .js extension in import path)
  • code contains __dirname, which doesn't work under esm. It's exclusively commonjs variable, there is the way how to do it in esm by import.meta.url
  • there are broken imports from a few external packages with default export.

I think there is no point in supporting esm in nexus-prisma until nexus, and prisma will implement such support as well.

Feel free to share your thoughts about this.

@rostislav-simonik
Copy link
Collaborator

Created discussion #693 to follow up on this topic. I'll prepare fix to remove esm build.

@rostislav-simonik-nexus-prisma-admin
Copy link
Collaborator

🎉 This issue has been resolved in version 1.0.18 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released type/bug Something is not working the way it should
Projects
None yet
3 participants