tamayura_toki
@tamayura_toki

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

firebaseにnext.jsアプリをデプロイしたいがエラーが発生する

解決したいこと

このサイトを参考にfirebaseにnext.jsをデプロイしたいです。

このようなエラーが出てしまいnpm run serveができません

TypeError: next is not a function
    at Object.<anonymous> (/home/XXXXX/with-firebase-hosting-app/firebaseFunctions.js:7:22)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at loadModule (/home/XXXXX/with-firebase-hosting-app/node_modules/firebase-functions/lib/runtime/loader.js:40:16)
    at loadStack (/home/XXXXX/with-firebase-hosting-app/node_modules/firebase-functions/lib/runtime/loader.js:93:23)
    at /home/XXXXX/with-firebase-hosting-app/node_modules/firebase-functions/lib/bin/firebase-functions.js:56:56
    
⬢  functions: Failed to load function definition from source: FirebaseError: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error

助力頂きたいです。

該当するソースコード

当コードが悪さをしているのは理解できますが、何故悪さをしているのかが理解できず。

firebaseFunctions.js
const { join } = require('path')
const { https } = require('firebase-functions')
const { default: next } = require('next')

const nextjsDistDir = join('src', require('./src/next.config.js').distDir)

const nextjsServer = next({
  dev: false,
  conf: {
    distDir: nextjsDistDir,
  },
})
const nextjsHandle = nextjsServer.getRequestHandler()

exports.nextjsFunc = https.onRequest((req, res) => {
  return nextjsServer.prepare().then(() => nextjsHandle(req, res))
})

自分で試したこと

package.jsonのfirebase関連のバージョンが古いので新しくした上でnpm iをしました。
(nextjsは対応しているバージョンが13.4.7なのでそちらに合わせています。)

package.json
{
  "private": true,
  "main": "firebaseFunctions.js",
  "scripts": {
    "dev": "next src/",
    "build": "next build src/",
    "start": "next start src/",
    "serve": "npm run build && firebase emulators:start --only functions,hosting",
    "shell": "npm run build && firebase functions:shell",
    "deploy": "firebase deploy --only functions,hosting",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "^9.4.2",
    "firebase-functions": "^3.13.1",
    "next": "latest",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.3",
    "firebase-tools": "^9.3.0"
  }
}

package.json
{
  "private": true,
  "main": "firebaseFunctions.js",
  "scripts": {
    "dev": "next src/",
    "build": "next build src/",
    "start": "next start src/",
    "serve": "npm run build && firebase emulators:start --only functions,hosting",
    "shell": "npm run build && firebase functions:shell",
    "deploy": "firebase deploy --only functions,hosting",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "^11.11.0",
    "firebase-functions": "^4.5.0",
    "next": "13.4.7",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^3.1.0",
    "firebase-tools": "^12.8.0"
  }
}
0

1Answer

firebaseFunctions.js

const { default: next } = require('next')

const next = require('next')

に変更してみてはどうでしょうか?私はこれでエラーが解消されました。バージョン変更によるものでしょう。

3Like

Your answer might help someone💌