Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language specific files in TypeScript #109

Closed
ludans opened this issue Jun 3, 2023 · 8 comments
Closed

language specific files in TypeScript #109

ludans opened this issue Jun 3, 2023 · 8 comments
Assignees

Comments

@ludans
Copy link

ludans commented Jun 3, 2023

Hello,

I am trying to use language specific files, as described in https://github.com/forzagreen/n2words/wiki/Importing-only-specific-languages
In a typescript file, this statement does not work:
import n2words from 'n2words/i18n/EN';
I get this error: Impossible de localiser le module 'n2words/i18n/EN' ou les déclarations de type correspondantes.ts(2307)

If I use: import n2words from 'n2words/lib/i18n/EN';, I can compile, but I can an error at runtime: Package subpath './lib/i18n/EN' is not defined by "exports" in ... \node_modules\n2words\package.json

Could you please detail how to use this new packaging with TypeScript?

@TylerVigario
Copy link
Collaborator

TylerVigario commented Jun 3, 2023

@ludans Please let me know if adding // @ts-ignore just before the import line works for you?

// @ts-ignore
import n2words from 'n2words/i18n/EN'

Duplicate of #107

@ludans
Copy link
Author

ludans commented Jun 3, 2023

I get the following error at runtime:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\rosaenlg\rosaenlg\node_modules\n2words\lib\i18n\EN.js
require() of ES modules is not supported.
require() of C:\rosaenlg\rosaenlg\node_modules\n2words\lib\i18n\EN.js from C:\rosaenlg\rosaenlg\packages\english-ordinals\dist\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename EN.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\rosaenlg\rosaenlg\node_modules\n2words\package.json.

@TylerVigario
Copy link
Collaborator

TylerVigario commented Jun 3, 2023

@ludans Looks like a CommonJS environment. Does switching to dynamic import work?

// @ts-ignore
import('n2words/i18n/EN').then(n2words => {
    n2words.default(100)
})

@ludans
Copy link
Author

ludans commented Jun 4, 2023

I just use an index.ts file your code inside of it.
I can compile. When I run, I get:

[DEP0148] DeprecationWarning: Use of deprecated folder mapping "./i18n/" in the "exports" field module resolution of the package at C:\rosaenlg\rosaenlg\node_modules\n2words\package.json.
Update this package.json to use a subpath pattern like "./i18n/*".
(Use `node --trace-deprecation ...` to show where the warning was created)
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\rosaenlg\rosaenlg\node_modules\n2words\lib\i18n\EN.js
require() of ES modules is not supported.
require() of C:\rosaenlg\rosaenlg\node_modules\n2words\lib\i18n\EN.js from C:\rosaenlg\rosaenlg\packages\english-ordinals\dist\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in tha
t package scope as ES modules.
Instead rename EN.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\rosaenlg\rosaenlg\node_modules\n2words\package.json.

This is my tsconfig.json file:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "typeRoots": [
      "../../node_modules/@types"
    ],
    "sourceMap": true,
    "outDir": "dist",
    "declaration": true
  }
}

@TylerVigario
Copy link
Collaborator

TylerVigario commented Jun 4, 2023

@ludans Adding "moduleResolution": "node16" or "moduleResolution": "nodenext" to your tsconfig.json will stop the transform from import() to require().

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "moduleResolution": "nodenext"
    },
}
import('n2words/i18n/EN.js').then(({ default: n2words }) => {
    console.log(n2words(100));
});

However, I noticed that my export of the i18n languages is not working as intended. Going to send a PR shortly to fix that.

@TylerVigario
Copy link
Collaborator

TylerVigario commented Jun 4, 2023

@ludans The exports issue has been resolved in the latest release (1.16.2). Please let me know if the new release and the instructions in my previous comment help. Thanks for reporting!

EDIT: I've created a minimal test repo so I can proactively monitor the state of importing in TypeScript. You can check it out here.

@ludans
Copy link
Author

ludans commented Jun 5, 2023

thanks
let me first switch to node 16 and typescript 5.1.3 (which is useful, but looks less straightforward than I excepted)
and once these prerequisites work then I will test again

@TylerVigario
Copy link
Collaborator

TylerVigario commented Jun 7, 2023

Cheers! I've added typings in #112 which is included in v1.16.3. // @ts-ignore should no longer be needed. Going to close this for now but feel free to open a new issue if anything comes up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants