Home > Net >  Deploy Nuxt 3 website to Firebase Hosting
Deploy Nuxt 3 website to Firebase Hosting

Time:08-22

I followed this article on deploying a Nuxt 3 website to Firebase Hosting.

So this is the content of my functions/index.js:

const functions = require("firebase-functions");
const { Nuxt } = require("nuxt-start");

// Require nuxt config
const nuxtConfig = require('./nuxt.config.js')

const config = {
    ...nuxtConfig,
    dev: false,
    buildDir: "nuxt",
};
const nuxt = new Nuxt(config);

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

This is my functions/package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "dev": "nuxt",
    "build": "nuxt build",
    "start": "nuxt start",
    "generate": "nuxt generate",
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
    "lint:js": "eslint --ext \".js,.ts,.vue\" --ignore-path .gitignore .",
    "lintfix": "npm run lint:js -- --fix"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.18.0",
    "nuxt-start": "^2.15.8",
    "@nuxt/content": "^1.15.1",
    "core-js": "^3.19.3",
    "nuxt": "^2.15.8",
    "vue": "^2.6.14",
    "vue-server-renderer": "^2.6.14",
    "vue-template-compiler": "^2.6.14",
    "vuetify": "^2.6.1",
    "webpack": "^4.46.0"
  },
  "devDependencies": {
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0",
    "@babel/eslint-parser": "^7.16.3",
    "@fortawesome/fontawesome-free": "^6.1.2",
    "@nuxt/types": "^2.15.8",
    "@nuxt/typescript-build": "^2.1.0",
    "@nuxtjs/eslint-config-typescript": "^8.0.0",
    "@nuxtjs/eslint-module": "^3.0.2",
    "@nuxtjs/google-fonts": "^3.0.0-0",
    "@nuxtjs/svg": "^0.4.0",
    "@nuxtjs/vuetify": "^1.12.3",
    "eslint-plugin-nuxt": "^3.1.0",
    "eslint-plugin-vue": "^8.2.0"
  },
  "private": true
}

nuxt.config.js:

import colors from 'vuetify/es5/util/colors'

module.exports = {
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    titleTemplate: '%s', //'%s - test',
    title: 'test',
    htmlAttrs: {
      lang: 'en'
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' },
      { name: 'format-detection', content: 'telephone=no' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [
  ],

  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
  ],

  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
    // https://go.nuxtjs.dev/typescript
    '@nuxt/typescript-build',
    // https://go.nuxtjs.dev/vuetify
    '@nuxtjs/vuetify',
    '@nuxtjs/svg'
  ],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // https://go.nuxtjs.dev/content
    '@nuxt/content',
    '@nuxtjs/google-fonts'
  ],

  // Content module configuration: https://go.nuxtjs.dev/config-content
  content: {},

  // Vuetify module configuration: https://go.nuxtjs.dev/config-vuetify
  vuetify: {
    defaultAssets: { icons: 'fa' },
    treeShake: true,
    customVariables: ['~/assets/variables.scss'],
    optionsPath: './vuetify.config.js',

  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
  }
}

I copied the .nuxt content into functions/nuxt as well as nuxt.config.js into functions directory.

Now when I run firebase deploy --only functions I am getting this error:

  • functions: Finished running predeploy script. i functions: ensuring required API cloudfunctions.googleapis.com is enabled... i functions: ensuring required API cloudbuild.googleapis.com is enabled... i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
  • artifactregistry: required API artifactregistry.googleapis.com is enabled
  • functions: required API cloudfunctions.googleapis.com is enabled
  • functions: required API cloudbuild.googleapis.com is enabled i functions: preparing codebase default for deployment

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

I already tried adding:

"type": "module"

to the functions/package.json. This leads to a different error message:

  • functions: Finished running predeploy script. i functions: ensuring required API cloudfunctions.googleapis.com is enabled... i functions: ensuring required API cloudbuild.googleapis.com is enabled... i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
  • functions: required API cloudfunctions.googleapis.com is enabled
  • artifactregistry: required API artifactregistry.googleapis.com is enabled
  • functions: required API cloudbuild.googleapis.com is enabled i functions: preparing codebase default for deployment

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

CodePudding user response:

Is this project working otherwise? Because I noticed that you said Nuxt3, but your project looks like a nuxt2 projects in some regards.

Stuff I noticed:

  • nuxt.config.js file instead of .ts
  • module.exports instead of export default defineNuxtConfig()

Does it work with a fresh nuxt3 project?

  • Related