Home > Software design >  Import and Require ES Module Incompability
Import and Require ES Module Incompability

Time:12-28

I installed two npm packages.

One is requiring me to use

var langs = require('langs');

And another is

import { franc } from "franc";

But I get an error that says require is not supported in ES module.

I tried using - import { langs } from "langs"; but it's not working for some reason.

Am I importing in the wrong way or the other package cannot be imported and only required?

Also, what does ES module mean? Here's the definition I've found: A module is a software component or part of a program that contains one or more routines.

It's like another language inside Javascript?

CodePudding user response:

Since Node v12 (April 2019), support for ES modules is enabled by default, and since Node v15 (October 2020) it's stable (see here). Files including node modules must either end in .mjs or the nearest package.json file must contain "type": "module". The Node documentation has a ton more information, also about interop between CommonJS and ES modules.

Performance-wise there is always the chance that newer features are not as well optimized as existing features. However, since module files are only evaluated once, the performance aspect can probably be ignored. In the end, you have to run benchmarks to get a definite answer anyway.

ES modules can be loaded dynamically via the import() function. Unlike required, this returns a promise.

package.json look like

enter image description here

CodePudding user response:

import langs from 'langs'

Can you try this one?

  • Related