Hey, guys~ I can't deploy when I assign a paths
arguments in tsconfig.json
, like this:
{
"compilerOptions": {
"lib": ["es6"],
"module": "commonjs",
"outDir": "lib",
"rootDir": "src",
"target": "es6",
"sourceMap": true,
"noImplicitReturns": true,
"baseUrl": ".",
// here, I assign a paths arguments
"paths": {
"@custom-modules/*": ["src/modules/*"],
}
},
"compileOnSave": true,
"include": ["src"]
}
The paths
arguments usually using TypeScript and it work perfectly. But firebase deploy throw that error:
Error: Error parsing triggers: Cannot find module '@custom-modules/moduleName'
This question has published in StackOverFlow before How to use TS Path Mapping with Firebase Cloud Functions, and for now, Can anyone help me? Thanks~ 🤣
As my understanding works, baseUrl
and paths
are only used by the compiler and linter to understand the typings and location of modules. Those two properties do _not_ tell the compiler to _include_ those mapped paths into the compilation or change the import
statements to work during compile time. Take a look at the compiled lib
directory to see that the @custom-modules/*
import
statement have not changed.
That said, the error is coming from the fact that in the deployment process, the CLI interprets the functions code to understand what functions need to be deployed. Since the functions code is requiring a module that it has no knowledge of (this is in the lib
folder, remember), an error is thrown that prints out that message.
If you are trying to save some typing by doing import ... from "@custom-modules/...";
and remapping to elsewhere in your structure, you can fix this by doing relative imports, which is probably the recommended way anyway. I don't believe baseUrl
and paths
are the answer you are looking for.
Hi there! Want to add more info for this issue, because I've encountered it as well.
In my other Node.js+TypeScript project the issue was in place, because, as it explained above, Typescript doesn't change require paths in output JS. The only working solution I was able to find is https://github.com/dividab/tsconfig-paths#with-node , which changes Node.js paths handling way based on tsconfig.json
config file. That works thanks to -r tsconfig-paths/register
argument passed to Node, and executing register
in runtime (even at the very top of index.ts) doesn't seem like to work in Firebase Functions.
Having relative paths seems very inconvenient for me, because I have kinda common
folder that is used also by frontend code, relative path to it would look like ../../../common
in index.ts, and even more ugly in subfolders.
So maybe there's a way to add this -r tsconfig-paths/register
argument for Firebase Functions, including both local and production environments?
I have found that https://www.npmjs.com/package/module-alias worked for me.
Make a new file /src/fixTsPaths.ts
:
import * as ModuleAlias from 'module-alias'
ModuleAlias.addAliases({
helpers: __dirname + '/helpers',
})
Import it in /scr/index.ts
.
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import './fixTsPaths'
Most helpful comment
Hi there! Want to add more info for this issue, because I've encountered it as well.
In my other Node.js+TypeScript project the issue was in place, because, as it explained above, Typescript doesn't change require paths in output JS. The only working solution I was able to find is https://github.com/dividab/tsconfig-paths#with-node , which changes Node.js paths handling way based on
tsconfig.json
config file. That works thanks to-r tsconfig-paths/register
argument passed to Node, and executingregister
in runtime (even at the very top of index.ts) doesn't seem like to work in Firebase Functions.Having relative paths seems very inconvenient for me, because I have kinda
common
folder that is used also by frontend code, relative path to it would look like../../../common
in index.ts, and even more ugly in subfolders.So maybe there's a way to add this
-r tsconfig-paths/register
argument for Firebase Functions, including both local and production environments?