是否可以使Node NOT Require the".js"?进口延期? [英] Is it possible to make Node NOT Require the ".js" Extension For Imports?

查看:50
本文介绍了是否可以使Node NOT Require the".js"?进口延期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node现在具有对导入的内置支持,这非常棒.但是这种支持要求您指定文件扩展名,这很烦人.

Node now has built-in support for imports, which is awesome. But that support requires you to specify the file extension, which is annoying.

我敢肯定为什么有这个理由(可能与他们对 .mjs 扩展名的怪异痴迷有关),但是有什么办法可以解决它并制作 import 的工作方式像正常"一样(您可以在其中关闭 .js )?

I'm sure there's some justification for why this is (likely having to do with their weird obsession with the .mjs extension), but is there any way to work around it and make import work "like normal" (where you can leave .js off)?

推荐答案

我已通过 esm 包修复了此问题.

I fixed this was with the esm package.

简而言之,Node组织在推出ES模块支持方面做得很糟糕,但是值得一提的是,与此同时,制作Lodash库的人已经添加了更好的支持,而且超级容易获得

In short, the Node organization has done an awful job of rolling out ES Module support, but the saving grace is that in the meantime the guy who made the Lodash library has already added far better support, and it's super easy to get.

两个步骤:

  1. npm我esm
  2. node -r esm index.js (而不只是 node index.js )
  1. npm i esm
  2. node -r esm index.js (instead of just node index.js)

如果您只是执行上述操作,则不需要任何特殊的Node标志(例如 experimental-modules )或其他任何东西,也不需要ES模块语法( import / export )将在您的应用程序中完美运行……包括在导入时不需要 .js .

If you just do the above you don't need any special Node flags (like experimental-modules) or anything else, and ES Module syntax (import/export) will work perfectly in your application ... including not requiring .js in imports.

这篇关于是否可以使Node NOT Require the".js"?进口延期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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