ค้นหาเว็บไซต์

typescript - ได้รับ "Error: Cannot find module <module>" เมื่อพยายามนำเข้าแพ็กเกจที่สร้างเองและรันอีมูเลเตอร์


ฉันได้สร้างโค้ดบางส่วนซึ่งฉันต้องการใช้ในฟังก์ชัน Firebase ของฉัน

นี่คือแพ็กเกจส่วนตัวที่ฉันกำลังดึงมาจาก GitHub ดังนั้นใน package.json ของฉันฉันจะมี

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

ตามเอกสารแล้ว สิ่งนี้ควรจะโอเค

อย่างไรก็ตาม การรันอีมูเลเตอร์จะไม่สามารถทำงานได้เพราะไม่พบโมดูล:

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.

หลังจากรันคำสั่ง build ฉันจะได้รับผลลัพธ์ที่สร้างไว้ใน 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");
// ...

แต่ว่าความผิดพลาดยังคงเหมือนเดิม

เพื่อความชัดเจน: การพึ่งพา @company/package ของฉันถูกติดตั้งอย่างถูกต้องภายใต้ node_modules/@company/package และมันมีไฟล์ api.ts ซึ่งมีโค้ดที่ฉันต้องการอยู่

ฉันจะนำเข้าห้องสมุดของฉันที่นี่ได้สำเร็จอย่างไร?

tsconfig.json

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

คำตอบ #1

ตรวจสอบให้แน่ใจว่าไฟล์ node_modules/@company/package/api.js มีอยู่และส่งออกโมดูล javascript

คำตอบ #2

ดังนั้น ปัญหาจึงเกิดจากความจริงที่ว่าฉันไม่ได้รัน npm run build ในเวิร์กโฟลว์ของ Github Action ที่รับผิดชอบในการเผยแพ็กเกจ

- 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

ปัญหาไม่ได้เกี่ยวข้องกับ Firebase หรือวิธีที่ฉันนำเข้าแพ็กเกจเลย - แพ็กเกจเอง ในแง่หนึ่ง ก็พัง

บทความที่เกี่ยวข้อง