Webpack: Uncaught SyntaxError: Unexpected token import

Created on 12 Jan 2017  路  13Comments  路  Source: vuejs-templates/webpack

On windows 10 with fresh new vue generated app, I get into issue when open in browser
localhost:8080 in console see this error:

main.js?3479:3 Uncaught SyntaxError: Unexpected token import
    at Object.<anonymous> (app.js:696)
    at __webpack_require__ (app.js:556)
    at fn (app.js:87)
    at Object.<anonymous> (app.js:587)
    at __webpack_require__ (app.js:556)
    at app.js:579
    at app.js:582

````

and this is what I see in console

DONE Compiled successfully in 1133ms

Listening at http://localhost:8080
````

node version v6.3.1
npm version 3.10.5

Most helpful comment

@learntoswim Your problem results from the fact that babel-loader is configured to transpile js files in /srconly, and you seem to have your mock files outside of that folder.

https://github.com/vuejs-templates/webpack/blob/master/template/build/webpack.base.conf.js#L66-L68

Just adjust the config to include the mocks folder and you are fine.

All 13 comments

This probably means that babel has not converted your code to ES5.

Do you have a .babelrc file in the project root? This one:

https://github.com/vuejs-templates/webpack/blob/master/template/.babelrc

If this file is missing, that's probably because vue-cli did not have the permissions required to create it.

I've also started seeing this error using the latest vue-cli install (2.8.0).

$ node -v
v6.9.1
$ npm -v
3.10.8
$ vue -V
2.8.0
$ vue init webpack my-project
$ cd my-project
$ npm i
$ npm run dev

Then I attempt to add a module, and I get a browser console error: Uncaught SyntaxError: Unexpected token export

Files:

mock/facebookAuth.js

export const facebookAuthMock = {
  accessToken: ...
  ...
};

src/main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import { facebookAuthMock } from '../mock/facebookAuth';

console.log(facebookAuthMock);

/* eslint-disable no-new */
new Vue({
  el: '#app',
  template: '<App/>',
  components: { App },
});

.babelrc

{
  "presets": ["es2015", "stage-2"],
  "plugins": ["transform-runtime"],
  "comments": false,
  "env": {
    "test": {
      "plugins": [ "istanbul" ]
    }
  }
}

package.json

  "devDependencies": {
    "autoprefixer": "^6.4.0",
    "babel-core": "^6.0.0",
    "babel-eslint": "^7.0.0",
    "babel-loader": "^6.0.0",
    "babel-plugin-transform-runtime": "^6.0.0",
    "babel-preset-es2015": "^6.0.0",
    "babel-preset-stage-2": "^6.0.0",
    "babel-register": "^6.0.0",
    "chalk": "^1.1.3",
    "connect-history-api-fallback": "^1.1.0",
    "css-loader": "^0.25.0",
    "eslint": "^3.7.1",
    "eslint-friendly-formatter": "^2.0.5",
    "eslint-loader": "^1.5.0",
    "eslint-plugin-html": "^1.3.0",
    "eslint-config-airbnb-base": "^8.0.0",
    "eslint-import-resolver-webpack": "^0.6.0",
    "eslint-plugin-import": "^1.16.0",
    "eventsource-polyfill": "^0.9.6",
    "express": "^4.13.3",
    "extract-text-webpack-plugin": "^1.0.1",
    "file-loader": "^0.9.0",
    "friendly-errors-webpack-plugin": "^1.1.2",
    "function-bind": "^1.0.2",
    "html-webpack-plugin": "^2.8.1",
    "http-proxy-middleware": "^0.17.2",
    "json-loader": "^0.5.4",
    "cross-env": "^3.1.3",
    "karma": "^1.3.0",
    "karma-coverage": "^1.1.1",
    "karma-mocha": "^1.2.0",
    "karma-phantomjs-launcher": "^1.0.0",
    "karma-sinon-chai": "^1.2.0",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-spec-reporter": "0.0.26",
    "karma-webpack": "^1.7.0",
    "lolex": "^1.4.0",
    "mocha": "^3.1.0",
    "chai": "^3.5.0",
    "sinon": "^1.17.3",
    "sinon-chai": "^2.8.0",
    "inject-loader": "^2.0.1",
    "babel-plugin-istanbul": "^3.0.0",
    "phantomjs-prebuilt": "^2.1.3",
    "chromedriver": "^2.21.2",
    "cross-spawn": "^4.0.2",
    "nightwatch": "^0.9.8",
    "selenium-server": "2.53.1",
    "semver": "^5.3.0",
    "opn": "^4.0.2",
    "ora": "^0.3.0",
    "shelljs": "^0.7.4",
    "url-loader": "^0.5.7",
    "vue-loader": "^10.0.0",
    "vue-style-loader": "^1.0.0",
    "vue-template-compiler": "^2.1.0",
    "webpack": "^1.13.2",
    "webpack-dev-middleware": "^1.8.3",
    "webpack-hot-middleware": "^2.12.2",
    "webpack-merge": "^0.14.1"
  },

This code works in the same project, but on the previous version of vue-cli

I have the same problem as @vaske . Same npm and node versions.

@learntoswim Your problem results from the fact that babel-loader is configured to transpile js files in /srconly, and you seem to have your mock files outside of that folder.

https://github.com/vuejs-templates/webpack/blob/master/template/build/webpack.base.conf.js#L66-L68

Just adjust the config to include the mocks folder and you are fine.

@LinusBorg That was it. Silly mistake on my behalf. Thank you.

Closing because of inactivity of OP

I have the same problem here; I just installed everything and got this error

@siempa So I ask you as well:

Do you have a .babelrc file in the project root? This one:
https://github.com/vuejs-templates/webpack/blob/master/template/.babelrc

And also; is the file with the error in the /src folder or outside of it?

I'v already read your comments up; I already have the .babelrc just like that one;
the file with the error is src/app.js

@LinusBorg yeah I have it as well.

Hm. Honestly, I have no further ideas. All I can say is that it seems that Windows 10 often play the role of a troublemaker in a lot of cases :(

@LinusBorg yeah I drop win completely and continue with it on Mac.

package.json

    "vue-loader": "^11.0.0",
    "vue-template-compiler": "^2.1.10",

This worked for me.

Was this page helpful?
0 / 5 - 0 ratings