简体   繁体   English

Socket.io-client 没有默认导出

[英]Socket.io-client no default export

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.我目前正在开发一个聚合物 3 项目,其中一个组件需要导入 socket.io-client,但无论我尝试什么,我都无法让它工作。

I have tried:我试过了:

import io from 'socket.io-client';

what i get back:我得到什么:

Uncaught SyntaxError: The requested module '../../node_modules/socket.io-client/lib/index.js' does not provide an export named 'default'未捕获的语法错误:请求的模块“../../node_modules/socket.io-client/lib/index.js”不提供名为“default”的导出

same for this:同样的:

import io from 'socket.io-client/dist/socket.io.js';

what i get back:我得到什么:

Uncaught SyntaxError: The requested module '../../node_modules/socket.io-client/dist/socket.io.js' does not provide an export named 'default'未捕获的语法错误:请求的模块“../../node_modules/socket.io-client/dist/socket.io.js”不提供名为“default”的导出

I have also tried this:我也试过这个:

import * as io from 'socket.io-client'

what i get back:我得到什么:

ReferenceError: require is not defined at index.js:4参考错误:在 index.js:4 中未定义要求

and this:和这个:

import * as io from 'socket.io-client/dist/socket.io.js'

what i get back:我得到什么:

TypeError: Cannot read property 'Buffer' of undefined类型错误:无法读取未定义的属性“缓冲区”

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 中的代码,代码中似乎没有使用任何 es6 导出,这可以解释为什么它确实不起作用。

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.我假设我可能使用了错误的构建或其他东西,但我不知道为什么会这样,因为我使用"socket.io-client": "^2.1.1"如果有人知道我做错了什么我'很高兴听到。

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. Polymer 需要使用 ES 模块 - 由于 socket.io-client 在 package.json ( https://github.com/rollup/rollup/wiki/pkg.module ) 中没有模块,Polymer 必须依赖于一个源用 ES 模块编写。 Socket.io-client provides neither. Socket.io-client 两者都不提供。 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 或你的模板之一中导入它,或者使用另一个库(或者用 webpack/gulp 做一些疯狂的事情)......

index.html索引.html

<script src="node_modules/socket.io-client/dist/socket.io.js"></script>

I have imported it after webcomponents import.我在 webcomponents 导入后导入了它。

In a component:在一个组件中:

const socket = io(...);

works.作品。

试试这个

 import * as socketIO from 'socket.io'

Inspecting the socket-io-client they have this so-called "UniversalModuleDefinition" that runs when the script is imported, you can see it here:检查 socket-io-client 他们有这个所谓的“UniversalModuleDefinition”,它在导入脚本时运行,你可以在这里看到它:

(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
    module.exports = factory();
else if(typeof define === 'function' && define.amd)
    define([], factory);
else if(typeof exports === 'object')
    exports["io"] = factory();
else
    root["io"] = factory();
})(this, function() {
    ... //does io setup factory presumably
}

The problem问题

If we try to import using import './socket.io.js' , ( ie we just are using vanilla js and we are not using webpack, amd, or requirejs ) then root is undefined.如果我们尝试使用import './socket.io.js' ,(即我们只使用 vanilla js 而我们没有使用 webpack、amd 或 requirejs ),那么root是未定义的。

One solution一种解决方案

Modify socket-io client js to check for this undefined root and set it to the window, like so:修改 socket-io 客户端 js 以检查这个未定义的根并将其设置为窗口,如下所示:

if(root === undefined){
    root = window;
}
root["io"] = factory();

Then you can simply do import './socket.io.js' and you will have io() in global scope.然后你可以简单地做import './socket.io.js'并且你将在全局范围内拥有io()

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

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