NodeJS“必须使用导入来加载ES模块". [英] NodeJS "Must use import to load ES Module"

查看:2055
本文介绍了NodeJS“必须使用导入来加载ES模块".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 hello.js 中将 myArr 导入到 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:

  1. 节点版本> =14.仅适用于最新版本的节点.
  2. 确保您的package.json中包含"type":"module"行,如果没有此行,则节点将假定您要使用Common JS模块而不是ESM.

这篇关于NodeJS“必须使用导入来加载ES模块".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆