简体   繁体   中英

Import and Require ES Module Incompability

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?

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

在此处输入图像描述

import langs from 'langs'

Can you try this one?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM