NodeJS“必须使用import来加载ES模块" [英] NodeJS "Must use import to load ES Module"
本文介绍了NodeJS“必须使用import来加载ES模块"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将 myArr
从 hello.js 导入到 index.js.但是我得到一个错误
I'm trying to import myArr
from hello.js into index.js. However I get an error of
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module
hello.js
export let myArr = ['hello', 'hi', 'hey'];
index.js
import { myArr } from './hello.js';
console.log(myArr);
我哪里出错了?
推荐答案
我运行你的代码没有问题.检查两件事:
I ran your code with no problem. Check for 2 things:
- 节点版本 >= 14.它只适用于最新版本的节点.
- 确保您的 package.json 包含一行type":module",如果没有此行,节点假定您要使用 Common JS 模块而不是 ESM.
这篇关于NodeJS“必须使用import来加载ES模块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文