gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:13:25 25 4
gpt4 key购买 nike

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

enter image description here

我在 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 将编译而不会出现任何错误。


共享/通用/index.ts:

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

共享/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/**/*"
]
}

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

最佳答案

我认为,您必须为 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";

关于typescript - Firebase 部署 - 找不到本地依赖项的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54816967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com