Reputation: 616
I am very new to ES6 so forgive me if this a duplicate question. As the title states I'm currently receiving the error:
Uncaught SyntaxError: Unexpected token import
Obviously the object Person has not been successfully imported for use in the app.js
file.
I've looked at example code across the web however still not working. I suspect its to do with the webpack.config.js
settings.
I have also tried using let person = require("./modules/Person");
but it does not work.
Please any ideas? They'll be much appreciated.
I've provide code excerpts below:
src/js/modules/person.js
class Person {
constructor(name, jobtitle, sex){
this.name = name;
this.jobtitle = jobtitle;
this.message = document.getElementById("message").html;
this.sex = sex;
}
Greet(){
console.log("Hello, my name is "+ this.name + " and i am a " + sex);
}
EchoOccupation(){
console.log("I am a "+ this.jobtitle);
}
EchoMessage(){
console.log("The message is "+ this.message);
}
}
module.exports = Person;
src/js/app.js
import { Person } from './modules/person';
let vic = new Person("Fred", "Web Developer", "male");
vic.Greet();
vic.EchoOccupation("Web Developer");
webpack.config.js
var path = require("path"),
watchBabel = require("watch-babel"),
srcJsDir = "./src/js/",
srcDestJsDir = "dist/assets/js/",
options = { glob: "src/*.js" },
watcher = watchBabel(srcJsDir, srcDestJsDir, options);
watcher.on("ready", function() {
console.log("ready");
}).on("success", function(filepath) {
console.log("Transpiled ", filepath);
}).on("failure", function(filepath, e) {
console.log("Failed to transpile", filepath, "(Error: ", e);
}).on("delete", function(filepath) {
console.log("Deleted file", filepath);
});
module.exports = {
entry: [srcJsDir + "/app.js"],
output: {
path: srcDestJsDir,
filename: "app.js"
},
watch: true,
devServer: {
inline: true,
port: 8000
},
module: {
loader : [
{
test: /\.js$/,
exclude: "/node_modules/",
loaders: ['babel', 'watch-babel'],
query: {
presets: ['es2015'],
plugins: ['babel-plugin-uglify']
}
},
]
},
resolveLoader: {
root: path.join(__dirname, 'node_modules/')
}
};
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>im loving es6</title>
<link href="/src/css/frameworks/bootstrap.min.css" />
</head>
<body>
<header></header>
<main>
<div class="container">
<div class="row">
<div class="col-xs-12">
<article>
<section>
<div id="app"></div>
<div id="message">Hello this is my message!</div>
</section>
<aside></aside>
</article>
</div>
</div>
</div>
</main>
<footer></footer>
<script type="text/javascript" src="/dist/assets/js/app.js"></script>
</body>
</html>
Upvotes: 1
Views: 2973
Reputation: 12047
The error occurs because the module exported from person.js
does not have a Person
property.
You can fix this by making three changes:
src/js/modules/person.js
class Person {
...
}
// Set the Person class as the object exported from this module
export default Person;
src/js/app.js
// Import the Person class
import Person from './modules/person';
webpack.config.js
...
module: {
// Rename the 'loader' property to 'loaders'
loaders: [
...
]
}
You'll find more information on import
here: ES6 import
Upvotes: 4