forzagreen / n2words

Convert numerical numbers to written numbers, in 25+ languages.
MIT License
87 stars 22 forks source link

Error: Cannot find module 'n2words' or its corresponding type declarations #127

Closed michalgrzyska closed 9 months ago

michalgrzyska commented 1 year ago

Thing happens when writing Firebase Functions. I installed n2words with npm i and have:

image

When I try with // @ts-ignore it compiles, but functions return:

Failed to initialize and load triggers. This shouldn't happen: require() of ES Module [mypath]\functions\node_modules\n2words\lib\n2words.js from [mypath]\functions\lib\tax-computing\money-to-words.js not supported.
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.

Any ideas how to fix that?

TylerVigario commented 1 year 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?

TylerVigario commented 1 year ago

I will close this issue, but feel free to comment further or open a new one.

michalgrzyska commented 1 year ago

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: image

VSCode shows only: image

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 😒

SmashinFries commented 1 year ago

You can import it like this (ES6/ES2015):

import n2words from 'n2words/lib/i18n/en'
michalgrzyska commented 1 year ago

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)
TylerVigario commented 1 year ago

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?

michalgrzyska commented 1 year ago

@TylerVigario sure. Here is an example as my public repo: https://github.com/michalgrzyska/n2words-error-sample

TylerVigario commented 1 year ago

@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.

michalgrzyska commented 1 year ago

Thanks a lot. There may probably be some problem with my different packages but I'll try to find substitutes.

michalgrzyska commented 1 year ago

@TylerVigario was it possible to make anything working?

TylerVigario commented 9 months ago

@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.