我有一个React项目,当我在chrome中打开它的路径时,它会在本地工作。但是,在尝试部署到Heroku (或运行npm start
)时,我收到以下错误:
import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
我搜索了StackOverflow和Heroku的支持页面,但一无所获。
这是我的package.json:
{
"name": "my-app-name",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "webpack",
"start": "node ./my-app-name.jsx",
"postinstall": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-cli": "^6.24.0",
"babel-core": "^6.24.0",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.0",
"babel-preset-react": "^6.23.0",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-redux": "^5.0.3",
"react-router": "^4.0.0",
"react-router-dom": "^4.0.0",
"redux": "^3.6.0",
"webpack": "^2.3.2"
},
"engines": {
"node": "6.2.1",
"npm": "4.4.4"
}
}
和我的webpack.config.js:
module.exports = {
entry: "./my-app-name.jsx",
output: {
filename: "./bundle.js"
},
module: {
loaders: [
{
test: [/\.jsx?$/, /\.js?$/],
exclude: /(node_modules)/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
},
devtool: 'source-map',
resolve: {
extensions: ["*",".js", ".jsx" ]
}
};
我的节点和npm版本与package.json中列出的版本相同。我已经进行了三次检查,以确保所有内容都安装正确,其他StackOverflow答案都没有帮助。当然,这肯定是巴别塔出了什么问题(因为它不能识别import
),但我就是想不出是什么问题。
发布于 2017-04-02 21:21:52
my-app-name.jsx
不是转译后的版本。您需要指向Node,而不是node ./my-app-name.jsx
来启动像下面这样的转换后的构建
https://stackoverflow.com/questions/43173512
复制相似问题