question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It is not possible to use this package in an ES module because of the use of __dirname in the code: https://github.com/mashpie/i18n-node/blob/v0.15.0/i18n.js#L125. What it be possible to use another approach?

Or export a file that can be used by a ES module project

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mashpiecommented, Dec 21, 2022

__dirname is CJS only while import.meta.url is ESM only. So i18n would need some transpiler to support both package types… eventually there’ll be major release for ESM only dropping CJS support as proposed by https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

For now you might try with explicitly setting directory instead of falling back to defaults, ie.:

import { fileURLToPath } from 'node:url';
import path from 'node:path';
import { I18n } from 'i18n';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const i18n = new I18n({
  locales: ['en', 'de'],
  directory: path.join(__dirname, 'locales')
});

now you get back full control about your locales directory setting

0reactions
mashpiecommented, Dec 21, 2022

do you have a reproduction repo? I just tried both https://github.com/mashpie/i18n-esm-cjs-example and don’t get any errors

Read more comments on GitHub >

github_iconTop Results From Across the Web

ES modules: A cartoon deep-dive - Mozilla Hacks - the Web ...
Modules give you a better way to organize these variables and functions. With modules, you group the variables and functions that make sense...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
ECMAScript modules are the official standard format to package JavaScript code for reuse. Modules are defined using a variety of import and export...
Read more >
ES Modules | Dev Cheatsheets - Michael Currin
As of ES6 (ES2015), JavaScript supports a native module format called ES Modules, or ECMAScript Modules. This is modern way to do modules...
Read more >
CommonJS vs ES Modules in Node.js - A Detailed Comparison
ES Modules are a new module system developed by Google which allows for greater modularity and reuse across different programming languages.
Read more >
CommonJS vs. ES modules in Node.js - LogRocket Blog
The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found