【问题标题】:Firebase deploy - Cannot find module of a local dependencyFirebase 部署 - 找不到本地依赖项的模块
【发布时间】:2019-07-15 22:49:00
【问题描述】:

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

我在backend/package.json 中添加了本地依赖项shared,如下所示:

"dependencies": {
    ...
    "shared": "file:../shared"
}

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

firebase deploy --only functions

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

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

Try running "npm install" in your functions directory before deploying.

这个错误是由于这一行:

import { currentWeek } from 'shared/common';

如果我将目录更改为../../../shared/common,firebase 将编译没有任何错误。


shared/common/index.ts:

export { currentWeek } from './current-week';

shared/tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "strict": true,
    "removeComments": true
  }
}

后端/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/**/*"
  ]
}

如果我有这个模块,为什么会出现这个错误?我有什么遗漏吗?

【问题讨论】:

  • 你有没有想过解决这个问题?遇到同样的问题
  • @Jonovono 不,我必须相对导入它(../../)

标签: typescript google-cloud-functions firebase-cli


【解决方案1】:

简而言之:

  1. node_modules 默认被忽略,backend/dist 被上传。
  2. npm run build 发生在本地,npm install 发生在云端。
  3. npm install 可能在云中失败,因为缺少 shared 文件夹或由于云环境中不同的目录结构和/或不同的“根”目录导致的路径错误。

或者,更详细地说:

Functions 有一个预部署步骤,在本地构建 (npm run build => tsc) typescript 项目,然后将输出推送到安装了依赖项的云端 (npm install)。

当您通过相对路径 (../../shared) 包含 shared 时,编译后的代码会在项目构建(本地)后输出到 backend/dist 文件夹并因此被上传。

当您通过本地节点模块依赖项包含shared 时,输出在依赖项安装后位于backend/node_modules 文件夹中安装,因此它不会上传,因为node_modules 会被忽略默认。

您看到的另一个问题是为什么npm install 无法在云中安装本地依赖项。这可能是由于 (1) 没有在 backend 旁边上传 shared 和/或云环境中的目录和相对路径结构与本地不同。

firebase.json

{
  "functions": {
    "predeploy": ["npm --prefix \"$RESOURCE_DIR\" run lint", "npm --prefix \"$RESOURCE_DIR\" run build"],
    "source": "functions",
    "ignore": ["**/.env", "**/.runtimeconfig.js", "**/.log", "**/node_modules/**"]
  },
  ...
}

【讨论】:

    【解决方案2】:

    我认为,您必须为 typescript 编译器配置 module-resolution

    对于您的情况:

    {
      "compilerOptions": {
        "baseUrl": ".", // This must be specified if "paths" is.
        "paths": {
          "shared/*": ["../shared/*"] // This mapping is relative to "baseUrl".
        }
      }
    }
    

    你可以用另一个名字命名shared

    {
          "compilerOptions": {
            "baseUrl": ".", // This must be specified if "paths" is.
            "paths": {
              "myLib/*": ["../shared/*"] // This mapping is relative to "baseUrl".
            }
          }
        }
    

    用法:

    import { currentWeek } from "myLib/common";
    

    【讨论】:

    • 如果我将shared 文件夹添加到paths,那么我也必须将它添加到include,这会导致同样的错误。唯一的区别是shared 不会出现在node_modules 中(这又引出了一个问题——为什么firebase 找不到它?我假设它在外部运行npm install 会导致404。