运行server.js
文件时,出现语法错误,内容大致如下:
文件
?
中的意外字符@babel\core\lib\transformation\normalize-file.js:209
当我尝试使用以下代码执行server.js
时:
import path from 'path';
import express from 'express';
var app = express();
var port = 3000;
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
但是如果我使用以下代码运行它就可以了:
import http from 'http';
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Express和Babel似乎有问题吗?
我将在下面添加package.json
和.babelrc
:
package.json
:
{
"name": "counter",
"version": "0.0.1",
"private": true,
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.1",
"@babel/node": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-plugin-dynamic-import-node": "^2.1.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"express": "^4.16.3",
"nodemon": "^1.18.4",
"path": "^0.12.7",
"react": "^16.5.1",
"react-dom": "^16.5.1",
"react-scripts": "^1.1.4"
},
"dependencies": {
"prop-types": "^15.6.1",
"react-redux": "^5.0.7",
"redux": "^3.5.2"
},
"scripts": {
"start": "nodemon --exec babel-node server\\server.js ",
"build": "react-scripts build",
"eject": "react-scripts eject",
"test": "react-scripts test"
}
}
.babelrc
:
{
"presets": ["@babel/preset-react","env"],
"plugins": ["@babel/plugin-syntax-dynamic-import","dynamic-import-node"]
}