省略文件扩展名,ES6模块NodeJS [英] Omit the file extension, ES6 module NodeJS
本文介绍了省略文件扩展名,ES6模块NodeJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试了解Node和ES模块.具体来说,如何/是否可以从import语句的路径字符串值中省略文件扩展名(并且可以选择使VSCode自动完成这些路径).
I'm trying to get a handle on Node and ES modules. Specifically how/if you can omit the file extension from the path string value of the import statement (and optionally get VSCode to autocomplete those paths).
我了解您可以为文件提供 .mjs
扩展名,也可以设置"type"=
中的模块" ,但是两种方法都会导致以下问题.
I understand you can either gives files the .mjs
extension or set "type" = "modules"
in the package.json
but both approaches lead to the following problems.
- 如果文件扩展名为
- VSCode不会自动完成路径,只有
.js
时,VSCode才会看到该文件.但是,如果它是.js
,则自动完成功能会忽略字符串中的扩展名,并且导入会失败,直到我手动添加它为止. - 尝试在我自己的模块中使用像
graphql
这样的库也失败了,因为graphql模块中.mjs
文件之间的所有导入语句均已写成省略了扩展名从字符串中获取.
.mjs
,- VSCode won't autocomplete the path if the file extension is
.mjs
, it only sees the file if it's.js
. However if it is.js
the autocomplete omits the extension from the string and the import fails until I add it manually. - Trying to use a library like
graphql
inside my own modules also fails because all the import statements between the.mjs
files in the graphql module have been written omitting the extension from the string.
SO ...何时不包括对ES6模块导入有效的扩展,是否有通过NodeJS启用此条件的方法?
SO... when is not including the extension valid with ES6 module imports, and is there anyway to get this condition enabled with NodeJS?
推荐答案
查看全文