Uncaught SyntaxError: Unexpected token { in import

2019-09-19 23:05发布

问题:

I'm attempting to put my first full-stack application together and am getting an unexpected syntax error:

"Uncaught SyntaxError: Unexpected token {"

The error is coming from this line of code in my map.js file:

import {userInput} from './algorithm/searchingAlgorithm.js';

ejs file:

<head>
    <script type="text/javascript" src="../javascripts/map.js"></script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCx0LvEwPUgGhpLjCErr24dOnk-VWjo83g&callback=initMap">
    </script>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>

from './algorithm/searchingAlgorithm.js':

export default async function userInput(origin, destination){

I've done a lot of searching and have yet to come up with an answer. I'm using node.js/express and express generator, javascript. I'm also utilizing the google maps api.

回答1:

The import and export statements are used frequently in web development, but they do not currently work automatically in a browser because they are newer features in the JavaScript language. You need to "build" the code into a format that the browser can execute. Example code on the web tends to assume that you are already doing this.

There are different tools that allow you to do this, such as Rollup or Webpack.