最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Socket.io-client no default export - Stack Overflow

programmeradmin3浏览0评论

I'm currently working on a project in polymer 3, one of the ponents needs to import socket.io-client but whatever i try i can't get it to work.

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'

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'

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

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.

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.

I'm currently working on a project in polymer 3, one of the ponents needs to import socket.io-client but whatever i try i can't get it to work.

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'

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'

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

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.

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.

Share Improve this question asked Sep 13, 2018 at 9:03 T99RotsT99Rots 3012 silver badges6 bronze badges 1
  • I'm heaving the same problem and I couldn't find an working webponent for it. – Emerson Bottero Commented Oct 11, 2018 at 14:50
Add a ment  | 

3 Answers 3

Reset to default 3

Try this one

import * as socketIO from 'socket.io'

Polymer requires the use of ES modules - since socket.io-client fails to have module in package.json (https://github./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

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

I have imported it after webponents import.

In a ponent:

const socket = io(...);

works.

Inspecting the socket-io-client they have this so-called "UniversalModuleDefinition" that runs when the script is imported, you can see it here:

(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', (i.e. we just are using vanilla js and we are not using webpack, amd, or requirejs) then root is undefined.

One solution

Modify socket-io client js to check for this undefined root and set it to the window, like so:

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.

发布评论

评论列表(0)

  1. 暂无评论