2

What's the ES6 style syntax for import binding? For example, socket.io requires an http server to be bound to. Here's an example in ES5 syntax:

var app = express()
var server = http.createServer(app)
var io = require('socket-io')(server) // Need ES6 syntax for this line

How would I achieve the same thing using ES6 syntax? I've tried:

import io from 'socket-io'(server)
import io(server) from 'socket-io'

All of which throw errors.

Jake Miller
  • 2,034
  • 1
  • 17
  • 38
  • 2
    You have to have node v10.1.0 and run with `node --experimental-modules` flag in order to use the ES6 syntax, but you'll need to break up those statements for your syntax to be valid, it won't be possible to inline the function invocation anymore. – Patrick Roberts May 09 '18 at 17:41
  • @PatrickRoberts Most likely the OP is using Babel to transpile the ES modules syntax to CJS. – Michał Perłakowski May 09 '18 at 18:00
  • @MichałPerłakowski Yeah I'm using babel. The linked duplicate answer is the solution I was looking for. Thanks! – Jake Miller May 09 '18 at 18:03

0 Answers0