【发布时间】:2016-06-20 10:57:36
【问题描述】:
当我开始学习 React 后尝试运行我的程序时,我在运行我的启动脚本时遇到了这个错误。我正在将类“App”从 app.js 导入到我的入口点 main.js。
下面是我的代码:
webpack.config.js
module.exports = {
entry: './main.js',
target: "electron",
output: {
path: './',
filename: 'app.js'
},
devServer: {
inline: true,
port: 3333
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
}
]
}
}
app.js:
const React = require('react');
class App extends React.Component {
mapboxgl.accessToken = 'key_here';
render() {
return (
<div>
var map = new mapboxgl.Map({
container: 'map',
style: 'map_style_here',
center: [-74.50, 40],
zoom: 9
});
</div>
);
}
}
export default App
main.js:
'use strict';
const React = require('react');
const ReactDOM = require('react-dom');
import App from './app';
const BrowserWindow = require('browser-window')
const app = require("app");
var mainWindow = null; // Keep global reference to main broswer window.
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});
app.on('ready', function() {
// Reference the main window object.
mainWindow = new BrowserWindow({width: 1200, height: 800});
mainWindow.loadURL('file://' + __dirname + "/app.html")
mainWindow.on('closed', function() {
// Dereference the main window object.
mainWindow = null;
ReactDOM.render(<App />, document.getElementById('map'))
})
})
错误:
Uncaught Exception:
SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:404:25)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at loadApplicationPackage
(C:\Programming\RestaurantChat\node_modules\electron
prebuilt\dist\resources\default_app\main.js:257:23)
at Object.<anonymous>
(C:\Programming\RestaurantChat\node_modules\electron
prebuilt\dist\resources\default_app\main.js:289:5)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
有什么东西没有编译吗?我真的不知道为什么会出现语法错误。
【问题讨论】:
-
编辑你的问题,你的语法错误说什么以及它是什么行会很有帮助!
-
不知道为什么我没有把它放在首位——我的错!给我一点时间来编辑。
-
抱歉,编辑之前耽搁了这么久,不得不接个电话。
-
好的,我认为
import App from './app'与var App = require('./app')相同,应该可以解决您的语法错误,但我不确定电子。 -
听起来这很可能是您如何编译 JS 的问题。你在使用 Webpack/Babel 吗?如果可以,您可以分享您的
webpack.config.js文件吗?
标签: javascript reactjs electron