为什么 package.json 文件中有“type: module"? [英] Why is 'type: module' in package.json file?

查看:1290
本文介绍了为什么 package.json 文件中有“type: module"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我升级了节点并构建了现有文件.

I upgraded the node and built the existing file.

但它没有构建,并且出现了错误.

But it didn't build, and there was an error.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:                          │
   │   ~~/nuxt.config.js                                      │
   │   require() of ES modules is not supported.                                            │
   │   require() of ~~/nuxt.config.js from                    │
   │   ~~/config.js is an ES   │
   │   module file as it is a .js file whose nearest parent package.json contains "type":   │
   │   "module" which defines all .js files in that package scope as ES modules.            │
   │   Instead rename nuxt.config.js to end in .cjs, change the requiring code to use       │
   │   import(), or remove "type": "module" from                                            │
   │   ~~/package.json.  

所以我删除了 package.json 文件中的 'type: module'.

So I removed 'type: module' in package.json file.

可以删除吗?

推荐答案

当 package.json 文件中有type: module"时,你的源代码应该使用 import 语法.当你没有时,你应该使用 require 语法.

When you have 'type: module' in the package.json file, your source code should use import syntax. When you do not have, you should use require syntax.

'type': 'module' 添加到 package.json 可以启用 ES 6 模块.有关详细信息,请参阅此处.

Adding 'type': 'module' to the package.json enables ES 6 modules. For more info, see here.

这篇关于为什么 package.json 文件中有“type: module"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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