【发布时间】:2019-08-25 08:46:41
【问题描述】:
我有以下用于反应应用程序的服务器代码(从创建反应应用程序创建):
import config from './../config/config'
import app from './express'
import mongoose from 'mongoose'
// Connection URL
mongoose.Promise = global.Promise
mongoose.connect(config.mongoUri)
mongoose.connection.on('error', () => {
throw new Error(`unable to connect to database: ${mongoUri}`)
})
app.listen(config.port, (err) => {
if (err) {
console.log(err)
}
console.info('Server started on port %s.', config.port)
})
其他服务器端文件也使用“导入”..
当尝试通过node server.js 运行它时,我收到以下错误:
(function (exports, require, module, __filename, __dirname) { import config from
'./../config/config'
^^^^^^
SyntaxError: Unexpected token import
我应该如何启动我的 react 应用程序,以便从一开始就运行客户端以及这个 server.js 服务器?,我通常会在 package.json 中使用以下内容:
"dev": "concurrently \"npm run react\" \"npm run server\"",
"react": "react-scripts start",
"server": "node src/server/index.js",
但是 node 命令在这种情况下不起作用。我试过这个没有成功: Using Import In NodeJS server
【问题讨论】: