在ES6和npm / bundlers的未来中使用npm模块 [英] Using npm modules with ES6 and Future of npm/bundlers
问题描述
我使用ES6已有一段时间,对npm或node.js没有任何经验。
I use ES6 for a while and have no experience with npm or node.js.
我想在使用ES6的项目中使用一些npm模块。
I want to use some npm modules in my project using ES6.
我尝试跟踪并出现错误
import {axios} from './axios.js';
请求的模块'./axios.min.js'不提供名为'axios'的导出
我正在尝试使用 https://github.com/axios/axios/blob/master/dist/axios.js
我不想使用< script src = ...< / script>
标记实现。我想按需使用ES6模块加载它。
I do not want to use <script src="..."></script>
tag implementation. I want to to load it on demand with ES6 modules.
是否有任何帮助脚本可以使npm模块适应ES6或任何解决方案?
Is there any helper script to adapt npm modules to ES6 or any solution?
推荐答案
import {axios} from './axios.js';
将要求Axios具有命名出口,如
would require Axios to have a named export like
export function axios(...params) { ... }
Axios确实有默认导出,您可以在不使用花括号的情况下进行导入:
Axios does have a default export though, which you import without the curly braces:
import axios from './axios.js';
要使导入在浏览器中工作,您需要使用 type = module
:
For imports to work in a browser, you need to declare the importing script with type="module"
:
<script type="module" src="./js/main.js"></script>
编辑:
检查该网址似乎Axios根本不提供ECMAScript模块。只要您使用诸如webpack之类的脚本来捆绑脚本,就可以导入我描述的方式。
Checking that URL you gave it seems Axios does not provide an ECMAScript module at all. Importing the way I described will work as long as you're using something like webpack to bundle your scripts.
我刚刚在Axios存储库中提出了有关以下主题的问题: https:/ /github.com/axios/axios/issues/1879
I have just filed an issue on the Axios repository regarding the topic: https://github.com/axios/axios/issues/1879
这篇关于在ES6和npm / bundlers的未来中使用npm模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!