Search code examples
javascriptnode.jsfirebasenestjs

sh: 1: nest: not found When deploying NestJS application to Firebase


Application is working locally just fine, but it just fails when I try to deploy it to Firebase functions. enter image description here

my package.json;

{
  "name": "questionsapi",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "node dist/main.js",
    "postinstall": "npm run build",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main.js",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "build:watch": "tsc --watch",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "dist/main.js",
  "engines": {
    "node": "18"
  },
  "dependencies": {
    "@nestjs/common": "^10.3.0",
    "@nestjs/config": "^3.1.1",
    "@nestjs/core": "^10.3.0",
    "@nestjs/microservices": "^10.3.0",
    "@nestjs/mongoose": "^10.0.2",
    "@nestjs/platform-express": "^10.3.0",
    "@nestjs/platform-socket.io": "^10.3.0",
    "@nestjs/schedule": "^4.0.0",
    "@nestjs/websockets": "^10.3.0",
    "@types/jsonwebtoken": "^9.0.5",
    "@types/ws": "^8.5.10",
    "axios": "^1.6.5",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "jsonwebtoken": "^9.0.2",
    "mongoose": "^8.1.0",
    "nats": "^2.19.0",
    "reflect-metadata": "0.1.13",
    "rimraf": "4.4.1",
    "rxjs": "^7.8.1",
    "ws": "^8.16.0",
    "firebase-admin": "^12.0.0",
    "firebase-functions": "^4.6.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.3.0",
    "@nestjs/schematics": "^10.1.0",
    "@nestjs/testing": "^10.3.0",
    "@types/cron": "^2.0.1",
    "@types/express": "^4.17.21",
    "@types/jest": "^29.5.11",
    "@types/node": "^20.11.5",
    "@types/supertest": "^6.0.2",
    "@typescript-eslint/eslint-plugin": "^6.19.0",
    "@typescript-eslint/parser": "^6.19.0",
    "eslint": "^8.56.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-prettier": "^5.1.3",
    "jest": "^29.7.0",
    "npm-run-all": "^4.1.5",
    "prettier": "^3.2.4",
    "supertest": "^6.3.4",
    "ts-jest": "^29.1.1",
    "ts-loader": "^9.5.1",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.3.3",
    "firebase-functions-test": "3.1.1"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

and firebase.json;

{
  "functions": [
    {
      "source": ".",
      "ignore": [
        "node_modules",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log"
      ],
      "predeploy": "npm --prefix \"$RESOURCE_DIR\" run build",
      "runtime": "nodejs18"
    }
  ]
}


Solution

  • This error indicates that nest cli command is not found in the firebase functions environment.

    This can be due to npm install ignoring devDependencies.

    Install @nestjs/cli with npm i --save @nestjs/cli to move this line

    "@nestjs/cli": "^10.3.0"
    

    from devDependencies to dependencies.

    Then attempt to deploy again.

    A similar answer can be found here