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

Nuxt.js 2 SSR部署Firebase Functions时ES模块兼容问题

部署Nuxt.js 2 SSR到Firebase Hosting/Functions的模块导入问题

我正尝试将Nuxt.js(2) SSR应用部署至Firebase Hosting/Functions,只要不在functions/src/index.ts中引入nuxt.config.ts就能成功部署,但我需要用到nuxt.config.ts中的元标签等配置。

初始代码

以下是我的functions/src/index.js文件(本质是Firebase针对.ts+nuxt导入的默认启动文件):

const { Nuxt } = require('nuxt-start')
const functions = require('firebase-functions')
const nuxtConfig = require('./../nuxt.config.ts')

const config = {
  ...nuxtConfig,
  dev: false,
  debug: false,
}

const nuxt = new Nuxt(config)

exports.ssrapp = functions.https.onRequest(async (req: any, res: any) => {
  await nuxt.ready()
  nuxt.render(req, res)
})

尝试过程及错误

尝试1:调整nuxt.config.ts的导出方式

不管在nuxt.config.ts中使用module.exports = { ... }还是export default { ... },都会返回以下错误:

Error: Failed to load function definition from source: Failed to generate manifest from function source: SyntaxError: Cannot use import statement outside a module

尝试2:添加"type": "module"到functions/package.json

在functions/package.json中添加"type": "module"(修改根目录package.json无效),得到错误:

Error: Failed to load function definition from source: Failed to generate manifest from function source: ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'path/to/my/project/functions/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

尝试将nuxt.config.ts中的export default改为module.exports = { ... },仍出现相同错误。

尝试3:改用import导入nuxt.config

将导入方式改为import * as nuxtConfig from './../nuxt.config',同时保留nuxt.config.ts中的module.exports = { ... },未成功,生成的代码一致,还在functions/lib内创建了src目录,使项目结构更复杂。

尝试4:恢复nuxt.config.ts的export default

将nuxt.config.ts改回export default { ... }并保留import * as nuxtConfig from './../nuxt.config',再次返回错误:

Error: Failed to load function definition from source: Failed to generate manifest from function source: ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'path/to/my/project/Doyban-Website/functions/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

尝试5:全用import语法

在functions/index.ts中全部使用import:

import * as functions from 'firebase-functions'
// @ts-ignore
import Nuxt from 'nuxt-start'
import * as nuxtConfig from './../nuxt.config'

const config = {
  ...nuxtConfig,
  dev: false,
  debug: false,
}

const nuxt = new Nuxt(config)

exports.ssrapp = functions.https.onRequest(async (req: any, res: any) => {
  await nuxt.ready()
  nuxt.render(req, res)
})

结果仍出现相同错误:

Error: Failed to load function definition from source: Failed to generate manifest from function source: ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'path/to/my/project/Doyban-Website/functions/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

我毫无头绪:-(


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 00:42:55