我正在使用webpack捆绑我的反应webapp和我的nodejs服务器。我的反应webapp捆绑很好(我使用的是import语句)。但是,当我尝试在我的nodejs服务器中添加import语句时,出现以下错误。
错误:
.../server/app.js:1
(function (exports, require, module, __filename, __dirname) { import Account from "./models/Account.js";
^^^^^^
SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/.../start.js:41:5)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
我的webpack.config.js:
"use strict";
const path = require("path");
const fs = require("fs");
let nodeModules = {};
fs.readdirSync("node_modules")
.filter(function(x) {
return [".bin"].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = "commonjs " + mod;
});
module.exports = [{
name: "client",
devtool: "source-map",
entry: "./src/app.js",
output: {
path: "./static/bin",
filename: "app.bundle.js"
},
module: {
loaders: [{
test: /\.jsx?$/,
include: /src/,
loader: "babel-loader",
query: {
presets: ["es2015", "react", "stage-0"]
}
},{
test:/\.scss$/,
include: /src/,
loaders: ["style", "css?sourceMap", "sass?sourceMap"]
}]
}
},{
name: "server",
target: "node",
devtool: "source-map",
entry: "./server/app.js",
output: {
path: "./server",
filename: "build.js"
},
externals: nodeModules,
module: {
loaders: [{
test: /\.jsx?$/,
include: /server/,
loader: "babel-loader",
query: {
presets: ["es2015", "react", "stage-0"]
}
},{
test: /\.json$/,
loader: "json-loader"
}]
}
}];
我的package.json:
{
"version": "1.0.1",
"main": "start.js",
"scripts": {
"start": "node --use_strict start.js",
"prestart": "npm install",
"postinstall": "webpack --display-error-details"
},
"devDependencies": {
"babel-eslint": "^6.0.4",
"eslint": "^2.11.1",
"eslint-plugin-react": "^5.1.1"
},
"dependencies": {
"babel-core": "^6.9.1",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"babel-relay-plugin": "^0.9.0",
"css-loader": "^0.23.1",
"express": "^4.13.4",
"express-graphql": "^0.5.3",
"graphql": "^0.6.0",
"graphql-relay": "^0.4.2",
"json-loader": "^0.5.4",
"mongoose": "^4.5.9",
"node-sass": "^3.8.0",
"react": "^15.1.0",
"react-dom": "^15.1.0",
"react-relay": "^0.9.0",
"react-router": "^2.4.1",
"sass-loader": "^4.0.0",
"style-loader": "^0.13.1",
"webpack": "^1.13.1"
}
}
我已尝试npm update
并删除并重新安装我的node_modules文件夹。
有什么想法吗?