Closed michalgrzyska closed 9 months ago
@michalgrzyska That particular issue seems to be an incompatibility between CommonJS (your project) require
and ESM (this project).
Instead change the require of n2words.js in [mypath]\functions\lib\tax-computing\money-to-words.js to a dynamic import() which is available in all CommonJS modules.
Have you tried using CommonJS Dynamic Import?
I will close this issue, but feel free to comment further or open a new one.
Sorry for my delay. Thanks for the reply - I tried to use n2words in the way that is shown in link you sent:
import('n2words/i18n/en.js').then(({default: n2words}) => {
console.log(n2words(100)) // "one hundred"
})
I converted it a bit to have a possibility of getting string as a result:
export async function moneyToWords(money: number): Promise<string> {
const n2wordsModule = await import("n2words/i18n/en.js");
const { default: n2words } = n2wordsModule;
return n2words(money);
}
But still I get:
VSCode shows only:
And my ts.config has
"module": "commonjs",
"target": "es2022",
I know this is strange, but Firebase Functions are very tricky and for some reason limited in some areas. I tried to set ES6 & NodeNext, but then something is not compiling in node_modules
in long
module 😒
You can import it like this (ES6/ES2015):
import n2words from 'n2words/lib/i18n/en'
You can import it like this (ES6/ES2015):
import n2words from 'n2words/lib/i18n/en'
I tried but it throws this:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/i18n/en' is not defined by "exports" in myprojectpath\functions\node_modules\n2words\package.json
at new NodeError (node:internal/errors:387:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:365:9)
at packageExportsResolve (node:internal/modules/esm/resolve:649:3)
at resolveExports (node:internal/modules/cjs/loader:522:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:562:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:971:27)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Module.require (node:internal/modules/cjs/loader:1057:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (myprojectpath\functions\lib\generate-invoice\money-to-words.js:4:14)
The correct import would be import n2words from 'n2words/i18n/en'
; however, this won't work in a CommonJS project.
@michalgrzyska Could you share a minimal repo that highlights the issue?
@TylerVigario sure. Here is an example as my public repo: https://github.com/michalgrzyska/n2words-error-sample
@michalgrzyska Thank you. I did a quick search and found that Firebase does support ESM. I'll try making it work with the repo you gave me and see what I find.
Thanks a lot. There may probably be some problem with my different packages but I'll try to find substitutes.
@TylerVigario was it possible to make anything working?
@michalgrzyska I know this is a late reply, and after the promise of figuring it out, I must apologize. Unfortunately, I won't be able to sort this out. As this is an issue outside of this package, I will close it for now. Like before, please revive this thread if something relevant pops up in the future.
Thing happens when writing Firebase Functions. I installed n2words with
npm i
and have:When I try with
// @ts-ignore
it compiles, but functions return:Any ideas how to fix that?