'Firebase Cloud Functions Cannot find module error in logs

I am trying to Serve Dynamic Content with Cloud Functions and NextJS.

Everything is working as expected locally when I run firebase serve.

But when I run firebase deploy and try and run my function I get the below error in the firebase functions logs.

enter image description here

Error: Cannot find module 'babel-runtime/core-js/object/get-prototype-of'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/user_code/next/dist/pages/_document.js:7:23)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

I am not directly using babel (I am using typescript) so one of my modules must be using it as a dependency.

I am guessing I have installed something globally on my local machine and it is not present in my cloud functions but how can I debug what this would be? I have tried adding babel-runtime to my package.json

node -v v6.11.5

package.json

{
  "name": "functions",
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start",
    "lint": "./node_modules/.bin/tslint -p tslint.json",
    "build-js": "./node_modules/.bin/tslint -p tslint.json && ./node_modules/.bin/tsc && cp package.json ../../build/functions/package.json"
  },
  "main": "index.js",
  "dependencies": {
    "async": "^2.6.0",
    "@babel/runtime": "^7.0.0-beta.39",
    "express": "^4.15.4",
    "firebase-admin": "~5.8.1",
    "firebase-functions": "^0.8.1",
    "googleapis": "^23.0.0",
    "next": "^4.2.1",
    "range-inclusive": "^1.0.2",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "request": "^2.83.0",
    "request-promise": "^4.2.2",
    "typescript": "^2.7.1"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.0.0-beta.39",
    "babel-runtime": "^6.26.0",
    "tslint": "^5.8.0",
    "typescript": "^2.7.1"
  },
  "private": true
}


Solution 1:[1]

Move babel-runtime from your devDependencies into your dependencies.

When you deploy, Google Cloud Functions only installs dependencies, it ignores devDependencies.

https://cloud.google.com/functions/docs/writing/specifying-dependencies-nodejs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pureth