Getting "Error: Cannot find module <module>" when trying to import custom package and running emulators

Viewed 696

I have generated some code which I want to use in my firebase functions.

It's a private package which I am pulling from github, so in my package.json I'd have

"dependencies": {
    "@company/package": "1.0.0"
    ...
}

According to the docs this should be fine.

However, running the emulators will not work because it cannot find the module:

$ firebase emulators:start --inspect-functions
i  emulators: Starting emulators: auth, functions, firestore, database, storage
⚠  functions: You are running the functions emulator in debug mode (port=9229). This means that functions will execute in sequence rather than in parallel.
⚠  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: hosting, pubsub
⚠  Your requested "node" version "14" doesn't match your global version "16"
i  firestore: Firestore Emulator logging to firestore-debug.log
i  database: Database Emulator logging to database-debug.log
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "/home/sfalk/workspaces/web-mobile/functions" for Cloud Functions...
>  Debugger listening on ws://localhost:9229/e09c9890-f15b-4634-b9bc-b8528f14c03d
>  For help, see: https://nodejs.org/en/docs/inspector
⚠  Error: Cannot find module '@technical-depth/nordigen-client-typescript/api'
Require stack:
- /home/sfalk/workspaces/web-mobile/functions/lib/index.js
- /usr/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (/home/sfalk/workspaces/web-mobile/functions/lib/index.js:5:15)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
⚠  We were unable to load your functions code. (see above)
   - You may be able to run "npm run build" in your functions directory to resolve this.

After running build I'd get the generated output in lib/index.js:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.helloWorld = void 0;
const functions = require("firebase-functions");
const api_1 = require("@company/package/api");
// ...

But the error stays the same.

Just to clarify: my @company/package dependency gets installed correctly under node_modules/@company/package and it contains an api.ts file where the code I require is contained.

How can I successfulle import my library here?

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}
2 Answers

Ensure that a file node_modules/@company/package/api.js exists and exports a javascript module.

So, the problem originated from the fact that I didn't run npm run build in the Github Action workflow which was responsible for publishing the package.

- name: Build client
  working-directory: .generated/
  run: |
    sudo npm install typescript@~4.3.5 --save
    sudo npm install
    sudo npm run build  # <--- The missing call for building the library

The issue was not related to Firebase or the way I imported the package - the package itself was, in a sense, broken.

Related