nextauthjs / next-auth

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

Cannot find module 'node_modules\next\headers' #10302

Closed OpenTextASOWSM closed 6 months ago

OpenTextASOWSM commented 8 months ago

Environment

System:
OS: Windows 11 10.0.22631
CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
Memory: 8.20 GB / 31.73 GB
Binaries:
Node: 20.9.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.19 - C:\Program Files\nodejs\yarn.CMD
npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD
pnpm: 8.15.4 - C:\Program Files\nodejs\pnpm.CMD
Browsers:
Edge: Chromium (122.0.2365.80)
Internet Explorer: 11.0.22621.1
npmPackages:
@auth/core: ^0.28.0 => 0.28.0
next: ^14.1.1 => 14.1.3
next-auth: ^5.0.0-beta.15 => 5.0.0-beta.15
react: ^18.2.0 => 18.2.0

Reproduction URL

https://github.com/OpenTextASOWSM/authjs-nextauth-5beta-repro

Describe the issue

Following the Authjs.dev Oauth tutorial witht he current npm next-auth/@beta adding it to an existing project I got errors:

Error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'node_modules\next\headers' imported from node_modules\next-auth\lib\index.js Did you mean to import next/headers.js?

Cause:

Seems to be mixed .js and .ts in libraries and some people have issues with locating correct files. See other places in auth.js core libraries where .js is added to explicitly select the right files, probably due to the same issue. Resolution: I made changes locally in my node_modules to temporarily resolve this.

node_modules/next-auth/lib/env.js

import {NextRequest} from "next/server.js";
import { setEnvDefaults as coreSetEnvDefaults } from "@auth/core";

-from-

import {NextRequest} from "next/server";
import { setEnvDefaults as coreSetEnvDefaults } from "@auth/core";

node_modules/next-auth/lib/index.js -to-

import { Auth } from "@auth/core";
import { headers } from "next/headers.js";
import { NextResponse } from "next/server.js";
import { reqWithEnvURL } from "./env.js";
import { createActionURL } from "./actions.js";

node_modules/next-auth/lib/actions.js

-to-

import { Auth, raw, skipCSRFCheck } from "@auth/core";
import { headers as nextHeaders, cookies } from "next/headers.js";
import { redirect } from "next/navigation.js";

after making changes restarting npm run dev I was able to use the sample code

How to reproduce

reproduction adding to an existing project not using the sample next-auth collection and using tutorials.

Expected behavior

Runs without throwing the error as it can identify files to include no matter if it has a .js or .ts extension

OpenTextASOWSM commented 8 months ago

Related to Pull request with fix - https://github.com/nextauthjs/next-auth/pull/10283

OpenTextASOWSM commented 8 months ago

moving to pull request https://github.com/nextauthjs/next-auth/pull/10304 as prior was on an branch, not merged to main.

OpenTextASOWSM commented 8 months ago

https://github.com/nextauthjs/next-auth/pull/10304 is waiting for reviewer

HazimAlper commented 7 months ago

Any update about this pr? I can not use the auth() in getServerSideProps function because of that.

NextJs version: 14.2.2 NextAuth version: 5.0.0-beta.16

These file changes seems fine. I tried on my local and finally I can use auth() in getServerSideProps function.

node_modules/next-auth/lib/actions.js From: Line 2 import { headers as nextHeaders, cookies } from "next/headers"; Line 3 import { redirect } from "next/navigation";

To: Line 2 import { headers as nextHeaders, cookies } from "next/headers.js"; Line 3 import { redirect } from "next/navigation.js";

node_modules/next-auth/lib/index.js From: Line 3 import { headers } from "next/headers"; Line 4 import { NextResponse } from "next/server";

To: Line 3 import { headers } from "next/headers.js"; Line 4 import { NextResponse } from "next/server.js";

node_modules/next-auth/lib/env.js From: Line 1 import { NextRequest } from "next/server";

To: Line 1 import { NextRequest } from "next/server.js";

As @OpenTextASOWSM said, the changes in this pr (#10304 ) seem to solve the problem. It deserves at least a review.

williamisnotdefined commented 6 months ago

Here i got the error: Did you mean to import "next/server.js"..

I saw that the commit was closed (https://github.com/nextauthjs/next-auth/pull/10304), is there any workaround to make it work?

OpenTextASOWSM commented 6 months ago

Seems they think root cause is in next.js not next-auth so aren't taking these fixes as work around. There is more commentary here: https://github.com/nextauthjs/next-auth/discussions/9385