Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code "Cannot find module" error on Firebase Functions Project

I set up a firebase functions project on visual studio code and inside the index.ts file I was unable to make it recognize the imported modules. The autocomplete shows that the modules exist, however when I import them I get the error "Cannot find module".

But the project compiles and passes the lint check. I just cannot use the intellisense. I tested the solutions below but cannot get it working;

  • Remove node-modules folder then execute npm install.
  • Create new firebase functions project on a new directory.
  • Uninstall and reinstall vs-code. (Removing every property inside AppData).
  • Reinstall the typescript globally.
  • Remove extensions.
  • Test on MacOS instead of Windows 10

Screenshots

  • The error

    enter image description here

  • Folder structure

    enter image description here

  • tsconfig.json

    {
      "compilerOptions": {
         "module": "commonjs",
         "noImplicitReturns": true,
         "noUnusedLocals": true,
         "outDir": "lib",
         "sourceMap": true,
         "strict": true,
         "target": "es2017",
         "typeRoots": ["node_modules/@types"] 
      },
      "compileOnSave": true,
      "include": ["src", "decs.d.ts"]
    }
    
    
  • package.json

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@google-cloud/firestore": "^4.2.0",
    "@google-cloud/storage": "^5.2.0",
    "@types/multer": "^1.4.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-multipart-file-parser": "^0.1.2",
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0",
    "multer": "^1.4.2"
  },
  "devDependencies": {
    "@types/cors": "^2.8.7",
    "firebase-functions-test": "^0.2.0",
    "tslint": "^5.12.0",
    "typescript": "^3.8.0"
  },
  "private": true
}
like image 758
Atakan Atamert Avatar asked Dec 09 '25 06:12

Atakan Atamert


1 Answers

Here is my use case.

in JS

const admin = require("firebase-admin")
const functions = require("firebase-functions")

in TS

import firebase from "firebase/app"
import "firebase/analytics"
import "firebase/auth"
import "firebase/storage"

in package.json

"dependencies": {
    "firebase": "^7.17.1",
    ...
  },

You can find official doc about ES2015 style import in the firebase document page https://firebase.google.com/docs/web/setup#add-sdks-initialize

ps. A screenshot is really helpful to describe your problem in general. Don't be care 🍻

like image 98
Fredric Cliver Avatar answered Dec 11 '25 19:12

Fredric Cliver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!