[英]How to use babel-core/register for writing ES6 code in node.js?
I was looking at this StackOverflow post which described how to use ES6 modules for production in Node. 我在看这个 StackOverflow帖子,它描述了如何在Node中使用ES6模块进行生产。 I tried replicating it, but for some reason something is going wrong. 我尝试复制它,但是由于某种原因出现了问题。
Here is my app.js
file: 这是我的app.js
文件:
import express from 'express'
const app = express()
app.get('/', (req, res) => {
res.send('Hello!')
})
export default app
And here is my server.js
: 这是我的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 TypeError:app.set不是函数
I imagine this is because my app
isn't being exported properly, but I don't get why that's the case. 我想这是因为我的app
未正确导出,但我不知道为什么会这样。 Any help would be appreciated! 任何帮助,将不胜感激! I am using Babel6 btw, and I have the es2015
preset being used. 我正在使用Babel6 btw,并且正在使用es2015
预设。
EDIT: It seems to work if I change export default app
to module.exports = app
... that's strange 编辑:如果我将export default app
更改为module.exports = app
...,这似乎很有效。这很奇怪
Babel 6 does not support 'export default' anymore (Babel 5 did). Babel 6不再支持“导出默认值”(Babel 5支持)。 You have to use this Babel plugin: 您必须使用以下Babel插件:
https://www.npmjs.com/package/babel-plugin-add-module-exports https://www.npmjs.com/package/babel-plugin-add-module-exports
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.