简体   繁体   English

2021年浏览器加载的ES6模块如何使用npm包?

[英]How to use npm packages as ES6 modules loaded by the browser in 2021?

I'm a JavaScript beginner.我是 JavaScript 初学者。 I do not use a "bundler".我不使用“捆绑器”。

For a few days now I've been trying to use moment.js and other date-time libraries in some JavaScript by importing it using ES6 modules (ESM).几天来,我一直在尝试通过使用 ES6 模块 (ESM) 导入某些 JavaScript 中的 moment.js 和其他日期时间库。

I have a JS module I wrote, which is transpiled from TS, and that has this line:我有一个我写的 JS 模块,它是从 TS 转换而来的,它有这样一行:

import moment from "../lib/moment/src/moment.js"

My module is itself loaded via an import from a <script type="module" > tag, which works.我的模块本身是通过从<script type="module" >标记的导入加载的,该标记有效。

I've discovered that the moment package contains what looks like "source" code in its src folder which seems to look more like the JS/TS I'm accustomed to, and has a "default export" at the bottom.我发现 package 在其src文件夹中包含看起来更像“源”代码的那一刻,看起来更像我习惯的 JS/TS,并且在底部有一个“默认导出”。

So I'm referencing that "source" version in my transpiled JS module.所以我在我转译的 JS 模块中引用了那个“源”版本。 This gets me past a couple of errors I was getting:这让我克服了我遇到的几个错误:

The requested module 'blah' does not provide an export named 'default'请求的模块“blah”不提供名为“default”的导出

And

Cannot set property 'moment' of undefined (at moment.js:10)无法设置未定义的属性“时刻”(在 moment.js:10)

And leaves me stuck with loading the other modules its dependent upon, because I guess, their file extensions are missing.并且让我坚持加载它所依赖的其他模块,因为我猜,它们的文件扩展名丢失了。

GET https://blah/lib/moment/src/lib/locale/locale net::ERR_ABORTED 404 (moment.js:37)获取 https://blah/lib/moment/src/lib/locale/locale net::ERR_ABORTED 404 (moment.js:37)


After 3 days tearing my hair out I feel like I have been fighting a battle I shouldn't be attempting at all. 3天后,我把头发扯掉了,我觉得我一直在打一场我根本不应该尝试的战斗。

I would expect in 2021, what with widespread ESM browser support, that this would just work, but I've tried 5 different date-time libraries and had no success.我希望在 2021 年,在广泛的 ESM 浏览器支持下,这会起作用,但我尝试了 5 个不同的日期时间库,但都没有成功。

I assume the 404s have occurred because the moment authors are NOT expecting people to be using their library like this, so they left off the file extensions knowing full well that it wouldn't load in a browser??我认为发生 404 是因为作者不希望人们像这样使用他们的库,所以他们完全知道它不会在浏览器中加载,所以他们放弃了文件扩展名?

Am I supposed to add an extra step in my client-side build process, Gulp, to add the extensions back on in the moment source code??我是否应该在我的客户端构建过程中添加一个额外的步骤,Gulp,以便在当前源代码中重新添加扩展?

Or am I doing something else wrong?还是我做错了什么?

Is it perhaps that everyone uses a "bundler" and these tools fix all this stuff somehow and so these issues never arise for 99% of web devs??是不是每个人都使用“捆绑器”并且这些工具以某种方式修复了所有这些东西,因此 99% 的 web 开发人员从未出现这些问题?

Thanks in advance.提前致谢。

You want to import a bundled version of the lib to be able to do that.您希望导入捆绑版本的 lib 以便能够做到这一点。 Try:尝试:

import from 'https://unpkg.com/moment@2.29.1/dist/moment.js' ;

You can download the bundled version and add to your project.您可以下载捆绑版本并添加到您的项目中。 Don't forget to put the license as well and check if they are at least MIT or similar.不要忘记也放许可证并检查它们是否至少是 MIT 或类似的。

If you want to refer to the source code you will certainly need to build that.如果您想参考源代码,您肯定需要构建它。 Specifically with libs that are using typescript.特别是使用 typescript 的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM