typescript - 得到“错误:找不到模块" 尝试导入自定义包和运行模拟器时
问题描述
我已经生成了一些我想在我的 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.
运行构建后,我将在以下位置获得生成的输出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"
]
}
解决方案
确保文件node_modules/@company/package/api.js
存在并导出javascript模块。
推荐阅读
- facebook-graph-api - Facebook Conversion API 的 Google Tag Manger 实现捐赠 Capture
- python - 使用分隔符删除数据
- android - Android 访问被拒绝查找属性“ro.hardware.chipname”
- html - 无法设置高度:0;如果图例可用,则为字段集
- r - 将进度表添加到 Shiny (shinydashboard/shinydashboardplus) 中的标题
- java - rich:dataScroller 不会在新搜索的第 1 页重置
- typo3 - 如果加载了其他扩展,则注入存储库
- php - 在 Eloquent 搜索期间添加自定义列
- javascript - 时刻格式返回“无效日期”
- .htaccess - Rewrite Rule keep showing Internal Server Error