Firebase 部署 - 找不到本地依赖项的模块

Eli*_*hen 11 typescript google-cloud-functions firebase-cli

我有一个名为的子模块shared,它位于backend文件夹(即云函数文件夹)旁边:

在此处输入图片说明

我已经添加了局部依赖sharedbackend/package.json像这样:

"dependencies": {
    ...
    "shared": "file:../shared"
}
Run Code Online (Sandbox Code Playgroud)

我跑了npm install并确保它node_modules/shared存在。虽然,当我运行以下代码时:

firebase deploy --only functions
Run Code Online (Sandbox Code Playgroud)

我收到以下错误(通过 firebase):

Error: Error parsing triggers: Cannot find module 'shared/common'

Try running "npm install" in your functions directory before deploying.
Run Code Online (Sandbox Code Playgroud)

此错误是由于此行:

import { currentWeek } from 'shared/common';
Run Code Online (Sandbox Code Playgroud)

如果我将目录更改为../../../shared/common,firebase 将编译而不会出现任何错误。


共享/公共/index.ts:

export { currentWeek } from './current-week';
Run Code Online (Sandbox Code Playgroud)

共享/tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "strict": true,
    "removeComments": true
  }
}
Run Code Online (Sandbox Code Playgroud)

后端/tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "declaration": true,
    "outDir": "./dist",
    "module": "commonjs",
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "target": "es6",
    "moduleResolution": "node",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2015",
      "dom"
    ]
  },
  "include": [
    "./src/**/*",
    "../shared/**/*"
  ]
}
Run Code Online (Sandbox Code Playgroud)

如果我有这个模块,为什么会出现这个错误?有什么我想念的吗?

hoa*_*gdv 1

我认为,你必须配置module-resolution打字稿编译器。

对于你的情况:

{
  "compilerOptions": {
    "baseUrl": ".", // This must be specified if "paths" is.
    "paths": {
      "shared/*": ["../shared/*"] // This mapping is relative to "baseUrl".
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您可以shared用其他名称命名。

{
      "compilerOptions": {
        "baseUrl": ".", // This must be specified if "paths" is.
        "paths": {
          "myLib/*": ["../shared/*"] // This mapping is relative to "baseUrl".
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

用法:

import { currentWeek } from "myLib/common";
Run Code Online (Sandbox Code Playgroud)