I'm having trouble getting my shared typescript library to work using an alias path with Firebase Functions.
The code compiles if I reference it using relative paths, but my functions/
directory uploads to Firebase Functions and can't use relative files outside of its directory.
functions/
- tsconfig.json
- src/*.ts
- lib/*.js
shared/
- tsconfig.json
- src/*.ts
- lib/*.ts
src/
- components/*.vue
tsconfig.json
tsconfig-base.json
In my function file, I try and reference one of my shared modules like so:
import { MyClass } from '@shared/src/MyClass'; // Error: Cannot find module '@shared/src/MyClass'
import { MyClass } from '../../shared/src/MyClass' // This Compiles, but fails deploying Cloud Functions
Because Cloud Functions needs to have all dependencies within the functions
directory, I'm unable to deploy the functions, even though it all compiles using relative paths.
What am I doing wrong with my setup, structure, or deployment?
I've also tried adding "@shared": "file:../shared"
to functions/package.json
as described here
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"sourceMap": true,
"strict": true,
"declaration": true,
"declarationMap": true,
"lib": [
"es2018"
],
"target": "es2018",
"types": [
"node"
]
},
"compileOnSave": true,
"files": [],
"include": [],
"exclude": [
"lib",
"node_modules"
]
}
{
"extends": "../tsconfig-base.json",
"compilerOptions": {
"composite": true,
"baseUrl": "./",
"outDir": "./lib",
"rootDir": "./src",
},
"compileOnSave": true,
"include": [
"src"
],
"references": [],
"exclude": [
"lib",
"node_modules"
]
}
{
"extends": "../tsconfig-base.json",
"references": [
{
"path": "../shared"
}
],
"compilerOptions": {
"baseUrl": "./",
"outDir": "./lib",
"rootDir": "./src",
"paths": {
"@shared/*": [
"../shared/*"
]
}
},
"compileOnSave": true,
"include": [
"src"
],
"exclude": [
"lib",
"node_modules"
]
}
My current workaround for anyone interested is to use webpack (or vue.config.js
in my case) to copy shared files into the functions
directory, and add functions/shared
to my .gitignore
.
Not the best approach, but it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With