要加载 ES 模块,请设置“type":“module"在 package.json 或使用 .mjs 扩展名 [英] To load an ES module, set "type": "module" in the package.json or use the .mjs extension

查看:242
本文介绍了要加载 ES 模块,请设置“type":“module"在 package.json 或使用 .mjs 扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在桌面上运行这个 repository vscode 扩展.

I am trying to run on desktop this repository a vscode extension.

我在本地克隆它并运行 npm install

I cloned it locally and run npm install

在 vscode 编辑器上按 f5 出现错误

Press f5 on vscode editor and got an error

Process exited with code 1
(node:1404) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
internal/process/warning:44
Canceled

为了解决这个警告,我发现了另一个 stackoverflow 问题 -(node:9374) 警告:加载 ES 模块, 设置类型":模块"

To work around with the warning I found another stackoverflow question - (node:9374) Warning: To load an ES module, set "type": "module"

所以我在 package.json 上设置了 "type":"module" 并再次按 f5.

So I set "type":"module" on package.json and press f5 again.

然后出现另一个错误.

Process exited with code 1
Uncaught TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /mnt/c/vscode-php-debug/src/phpDebug.ts

并在stackoverflow上发现了另一个问题 - 无法运行我的 Node.js Typescript 项目 TypeError [ERR_UNKNOWN_FILE_EXTENSION]: 未知文件扩展名.ts"对于/app/src/App.ts

And found another question on stackoverflow - Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts

所以我删除了类型":模块";发生的事情是我现在在循环中,混淆.

So I remove "type":"module" what happens is I'm in the loop now, confuse.

有没有人试过调试这个或遇到这样的?

Does anyone tried debugging this or encounter as such?

推荐答案

在节点 v14.16.1 中使用以下命令对我有用:

Using the following command worked for me with node v14.16.1:

node --loader ts-node/esm --experimental-specifier-resolution=node index.ts

有一个警告告诉我 --experimental-loader 是一个实验性功能,但我不在乎,因为我只用它来调试打字稿代码.

There is a warning telling me that --experimental-loader is an experimental feature but I don't care because I only use it to debug the typescript code.

这篇关于要加载 ES 模块,请设置“type":“module"在 package.json 或使用 .mjs 扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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