目前,我的 nodejs 服务器中的所有module都作为 require() 导入,即:
let path = require('path');
let express = require('express');
let http = require('http');
let app = express();
但是,我遵循的教程显示它们导入为:
import express from 'express'
import path from 'path'
哪个抛出错误:
SyntaxError: Unexpected token import
我的 webpack.config.js 设置为:
module: {
rules: [
{
test: /\.js?$/,
use: 'babel-loader',
exclude: /node_modules/
}
]
}
在 bablerc 中:
{
"presets": ["es2015", "react"]
}
我的软件包版本:
"babel-core": "^6.7.6",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"react": "^15.0.1",
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-preset-env": "0.0.3",
"webpack": "^2.2.1",
"webpack-dev-middleware": "^1.10.1",
"webpack-dev-server": "^2.4.1",
"webpack-hot-middleware": "^2.17.1"
}
导入适用于我所有的 react 组件文件,但不适用于 server.js。如何将我的服务器切换到从 require 导入?