Next.js 项目部署因 typescript 类型而失败

时间:2021-05-28 04:09:58

标签: typescript next.js vercel

我正在尝试通过 vercel 部署我的项目。我用 next.js 和 typescript 写的。

我使用 Next-authcredentials 提供程序进行身份验证。默认情况下,这个身份验证包有一个会话对象,如下所示:

{ name?: string; email?: string; image?: string; }。重点是:我在这个包的类型文件中向这个对象添加了一个 id 属性作为 string,我只想在会话中使用用户 ID。

一切正常,我可以将 id 用于后端,typescript 可以识别 id 属性,但在部署过程中出现错误。

所以这里有一些我在类型文件中添加了 id 属性的类型:

export interface DefaultUser {
  name?: string | null
  email?: string | null
  image?: string | null
  id?: string | null      // Here
}

export interface DefaultJWT extends Record<string, unknown> {
  name?: string | null
  email?: string | null
  picture?: string | null
  id?: string | null      // Here
  sub?: string
}

export interface DefaultSession extends Record<string, unknown> {
  user?: {
    name?: string | null
    email?: string | null
    image?: string | null
    id?: string | null      // Here
  }
  expires?: string
}

这是我的身份验证文件 (/api/auth/[...nextauth].js):

export default NextAuth({
  session: {
    jwt: true
  },

  providers: [
    Providers.Credentials({
      async authorize(credentials: Record<"email" | "password", string>, req) {
        try {
          return await authOperation(credentials); // this returns { id: "****", email: "****" }
        } catch (error) {
          throw error;
        }
      }
    })
  ],

  // these code are for add id property

  callbacks: {
    jwt: async (token, user, account, profile, isNewUser) => {
      if (user) {
        token.id = user.id;
      }
      return Promise.resolve(token);
    },

    session: async (session, user) => {
      session.user.id = user.id as string;
      return Promise.resolve(session);
    }
  }
});

这是我在 vercel 中遇到的错误:

20:22:45.371    Cloning github.com/mohammaDJ23/invoice (Branch: master, Commit: 575d08a)
20:22:45.621    Cloning completed: 249.901ms
20:22:45.659    Analyzing source code...
20:22:46.872    Installing build runtime...
20:22:49.742    Build runtime installed: 2.869s
20:22:52.630    Looking up build cache...
20:22:52.992    Build Cache not found
20:22:54.235    Installing dependencies...
20:23:04.676    npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules/fsevents):
20:23:04.676    npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
20:23:04.680    added 416 packages from 427 contributors in 9.896s
20:23:04.860    56 packages are looking for funding
20:23:04.860      run `npm fund` for details
20:23:04.908    Detected Next.js version: 10.2.2
20:23:04.911    Running "npm run build"
20:23:05.185    > my-app@0.1.0 build /vercel/path0
20:23:05.185    > next build
20:23:06.040    info  - Using webpack 5. Reason: no custom webpack configuration in next.config.js https://nextjs.org/docs/messages/webpack5
20:23:06.198    Attention: Next.js now collects completely anonymous telemetry regarding usage.
20:23:06.198    This information is used to shape Next.js' roadmap and prioritize features.
20:23:06.198    You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
20:23:06.199    https://nextjs.org/telemetry
20:23:06.248    info  - Checking validity of types...
20:23:13.456    Failed to compile.
20:23:13.456    ./pages/api/auth/[...nextauth].ts:32:20
20:23:13.456    Type error: Property 'id' does not exist on type '{ name?: string; email?: string; image?: string; }'.
20:23:13.457      30 | 
20:23:13.457      31 |     session: async (session, user) => {
20:23:13.457    > 32 |       session.user.id = user.id as string;
20:23:13.457         |                    ^
20:23:13.457      33 |       return Promise.resolve(session);
20:23:13.457      34 |     }
20:23:13.457      35 |   }
20:23:13.475    npm ERR! code ELIFECYCLE
20:23:13.475    npm ERR! errno 1
20:23:13.479    npm ERR! my-app@0.1.0 build: `next build`
20:23:13.479    npm ERR! Exit status 1
20:23:13.480    npm ERR! 
20:23:13.480    npm ERR! Failed at the my-app@0.1.0 build script.
20:23:13.480    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
20:23:13.487    npm ERR! A complete log of this run can be found in:
20:23:13.487    npm ERR!     /vercel/.npm/_logs/2021-05-27T15_53_13_480Z-debug.log
20:23:13.501    Error: Command "npm run build" exited with 1

1 个答案:

答案 0 :(得分:1)

你可以像下面这样使用check doc

session.userId = user.id

或 您可以尝试为会话使用任何类型。它会解决您的问题。

session: async (session:any, user) => {
    session.user.id = user.id as string;
    return Promise.resolve(session);
}