0

I was looking at this StackOverflow post which described how to use ES6 modules for production in Node. I tried replicating it, but for some reason something is going wrong.

Here is my app.js file:

import express from 'express'
const app = express()

app.get('/', (req, res) => {
  res.send('Hello!')
})

export default app

And here is my server.js:

require('babel-core/register')
const app = require('./app.js')
const port = 3000

app.set('port', port)

app.listen(port, () => {
  console.log(`Server listening on port ${app.get('port')}...`)
})

I thought I followed that post accurately, but I'm getting an error that says

TypeError: app.set is not a function

I imagine this is because my app isn't being exported properly, but I don't get why that's the case. Any help would be appreciated! I am using Babel6 btw, and I have the es2015 preset being used.

EDIT: It seems to work if I change export default app to module.exports = app... that's strange

Community
  • 1
  • 1
Saad
  • 49,729
  • 21
  • 73
  • 112

1 Answers1

2

Babel 6 does not support 'export default' anymore (Babel 5 did). You have to use this Babel plugin:

https://www.npmjs.com/package/babel-plugin-add-module-exports

orourkedd
  • 6,201
  • 5
  • 43
  • 66
  • That did the trick, thanks a lot! Do you know the reasoning behind why they aren't supporting it anymore? – Saad Jan 30 '16 at 04:54
  • Glad it helped. I don't know why; but I really like it and wish they had kept it – orourkedd Jan 30 '16 at 04:55
  • For what is worth, I have opted to use an es6 featureset that matches node 6. Makes unit testing really easy. – orourkedd Jul 05 '16 at 15:31