Socket.io-client没有默认导出 [英] Socket.io-client no default export
问题描述
我目前正在研究聚合物3中的一个项目,其中一个组件需要导入socket.io-client,但无论我尝试什么,我都无法让它工作。
I'm currently working on a project in polymer 3, one of the components needs to import socket.io-client but whatever i try i can't get it to work.
我试过:
import io from 'socket.io-client';
我得到的回报:
未捕获的SyntaxError:请求的模块
'../../node_modules/socket.io-client/lib/index.js'不提供名为'default'的
导出
Uncaught SyntaxError: The requested module '../../node_modules/socket.io-client/lib/index.js' does not provide an export named 'default'
与此相同:
import io from 'socket.io-client/dist/socket.io.js';
我得到的回报:
Uncaught SyntaxError:请求的模块
'../../node_modules/socket.io-client/dist/socket.io.js'没有
提供名为'的导出默认'
Uncaught SyntaxError: The requested module '../../node_modules/socket.io-client/dist/socket.io.js' does not provide an export named 'default'
我也试过这个:
import * as io from 'socket.io-client'
我得到的回报:
ReferenceError:在index.js中未定义require:4
ReferenceError: require is not defined at index.js:4
这个:
import * as io from 'socket.io-client/dist/socket.io.js'
我得到的回报:
TypeError:无法读取未定义的属性'Buffer'
TypeError: Cannot read property 'Buffer' of undefined
我后来看过了来自socket.io-client的代码,实际上似乎没有在代码中使用任何es6导出,这可以解释为什么它我ndeed不起作用。
I later on looked trough the code from socket.io-client and there really don't appear to be any es6 exports used in the code, that would explain why it indeed doesn't work.
我觉得奇怪的是,导入语法甚至在他们的网站上列为支持。我假设我可能使用了错误的构建或其他东西,但我不知道为什么会这样,因为我使用socket.io-client:^ 2.1.1
如果有人知道我做错了什么我会很高兴听到。
What i find weird tho is that the import syntax is even listed on their site as supported. I assume i may be using a wrong build or something but i don't know why that would be true as i use "socket.io-client": "^2.1.1"
if anyone knows what i'm doing wrong i'd be happy to hear.
推荐答案
聚合物需要使用ES模块 - 因为socket.io-client无法在package.json中安装模块( https:/ /github.com/rollup/rollup/wiki/pkg.module ),Polymer必须依赖于使用ES模块编写的源。 Socket.io-client既不提供。因此,您只能在index.html或其中一个模板中导入它或使用其他库(或者使用webpack / gulp做一些疯狂的事情)......
Polymer requires the use of ES modules - since socket.io-client fails to have module in package.json (https://github.com/rollup/rollup/wiki/pkg.module), Polymer must rely on a source which is written with ES modules. Socket.io-client provides neither. So you could only import it in index.html or one of your templates or use another library (or do some crazy thing with webpack / gulp)...
index。 html
index.html
<script src="node_modules/socket.io-client/dist/socket.io.js"></script>
我在webcomponents导入后导入了它。
I have imported it after webcomponents import.
在一个组件中:
const socket = io(...);
有效。
这篇关于Socket.io-client没有默认导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!