nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.43k stars 3.42k forks source link

Importing @auth/express causes errors #9840

Closed vtonchev closed 8 months ago

vtonchev commented 8 months ago

Environment

System: OS: macOS 14.2.1 CPU: (8) arm64 Apple M2 Memory: 109.30 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.11.0 - ~/.nvm/versions/node/v20.11.0/bin/node Yarn: 1.22.21 - ~/.npm-global/bin/yarn npm: 10.2.4 - ~/.nvm/versions/node/v20.11.0/bin/npm Browsers: Chrome: 121.0.6167.85 Safari: 17.2.1 npmPackages: @auth/express: ^0.4.0 => 0.4.0 next-auth: ^4.24.5 => 4.24.5

Reproduction URL

https://github.com/vtonchev/Reproducing--auth-express-error/tree/master

Describe the issue

I recieve the following error:

The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@auth/express")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/Users/vtonchev/Desktop/Projects/TractionControl/express-backend/package.json'.

I do not want to add field `"type": "module" inside package.json, because It create more error with other packages (ZenStackMiddleware).

How to reproduce

Just clone the repo and compile ts with command: tsc After that run the js file in dist folder: node {}Project_Dir}/dist/index.js

Expected behavior

everything to run correctly

balazsorban44 commented 8 months ago

The error message says it all. You can either add "type": "module", or rename the file to .mts.

Auth.js libraries are distributed as ESM-only.

You can reach out to zenstack, maybe they might consider switching to ESM too (recommended), or at least publish an ESM version as well:

https://github.com/zenstackhq/zenstack/blob/2b2bfcff965f9a70ff2764e6fbc7613b6f061685/tsconfig.base.json#L4

https://github.com/zenstackhq/zenstack/blob/2b2bfcff965f9a70ff2764e6fbc7613b6f061685/packages/server/package.json#L57-L69