You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nuxt 3生产构建后本地运行服务器遇AUTH_NO_ORIGIN错误求助

Nuxt 3 + @sidebase/core v0.1.4 构建后运行服务器出现 AUTH_NO_ORIGIN 错误解决方案

问题场景

使用@sidebase/core": "^0.1.4"开发Nuxt 3项目,执行构建后通过node .output/server/index.mjs启动本地服务器时,触发如下错误:

file:///C:/Users/user/Desktop/dir/project/.output/server/chunks/node-server.mjs:377
throw new Error(ERROR_MESSAGES.NO_ORIGIN);

Error: AUTH_NO_ORIGIN: No `origin` - 这是生产环境错误,详见https://sidebase.io/nuxt-auth/resources/errors,开发阶段可忽略
at getServerOrigin (file:///C:/Users/user/Desktop/dir/project/.output/server/chunks/node-server.mjs:377:9)
at file:///C:/Users/user/Desktop/dir/project/.output/server/chunks/node-server.mjs:400:5
at createNitroApp (file:///C:/Users/user/Desktop/dir/project/.output/server/chunks/node-server.mjs:988:5)
at file:///C:/Users/user/Desktop/dir/project/.output/server/chunks/node-server.mjs:992:18
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

已尝试操作:

  • 在.env文件添加AUTH_ORIGIN="http://localhost:3000/"
  • 在auth配置文件[...].ts中配置origin
  • 在nuxt.config.ts中配置origin

但问题仍未解决,以下是针对该版本的可行解决方案:


解决方案

1. 确保构建阶段注入环境变量

sidebase v0.1.4要求AUTH_ORIGIN在构建过程中就被加载,而非仅运行时提供。可通过以下方式实现:

  • 终端直接导出变量后构建:
    Windows:
    set AUTH_ORIGIN=http://localhost:3000 && npm run build
    
    Linux/macOS:
    AUTH_ORIGIN=http://localhost:3000 npm run build
    
  • 使用dotenv-cli加载.env构建:
    先安装依赖:
    npm install dotenv-cli --save-dev
    
    修改package.json的build脚本:
    "scripts": {
      "build": "dotenv -e .env nuxt build"
    }
    

2. 强制在nuxt.config.ts中指定origin

在配置文件中直接绑定环境变量或硬编码(仅本地测试用):

export default defineNuxtConfig({
  modules: ['@sidebase/core'],
  sidebase: {
    auth: {
      origin: process.env.AUTH_ORIGIN || 'http://localhost:3000'
    }
  }
})

3. 修正auth路由配置文件写法

确保server/api/auth/[...].ts中正确传递origin参数:

import { NuxtAuthHandler } from '#auth'
// 示例provider,替换为你使用的认证提供商
import GithubProvider from 'next-auth/providers/github'

export default NuxtAuthHandler({
  origin: process.env.AUTH_ORIGIN || 'http://localhost:3000',
  providers: [
    GithubProvider.default({
      clientId: process.env.GITHUB_CLIENT_ID,
      clientSecret: process.env.GITHUB_CLIENT_SECRET
    })
  ]
})

4. 运行时显式传递环境变量

如果构建阶段未注入,可在启动服务器时直接指定:
Windows:

set AUTH_ORIGIN=http://localhost:3000 && node .output/server/index.mjs

Linux/macOS:

AUTH_ORIGIN=http://localhost:3000 node .output/server/index.mjs

内容的提问来源于stack exchange,提问作者glux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 20:40:22