ParseError: 'import' and 'export' may appear only with 'sourceType: module'

Hi there!,

Hope you’re having a good time, I’m studying the HTML5 Games Novice to Ninja Book on Sitepoint, but, when I try to compile with npm this error appears:

ParseError: ‘import’ and ‘export’ may appear only with ‘sourceType: module’

It happens just after the npm initialization and Budo installation. I didn’t install Babel, Gulp o anything yet.

import pop from "../pop/index.js"; // ERROR
const { Container } = pop;

// Game setup code
const scene = new Container();

// Example game element to manipulate
const player = {
  update: function() {
    console.log("updated!");
  }
};

scene.add(player);
scene.update();
scene.remove(player);

console.log(scene.children);

I really don’t know what I’m doing wrong, so, any idea will be appreciated.

Thank’s in advance.

Regards,

Rodrigo

Finally it works. I did as follow:

Install browserify: npm install -g browserify

Install babel: npm install --save-dev browserify babelify babel-preset-es2015 babel-preset-stage-0 babel-preset-env babel-preset-react

Install babel/core: npm install @babel/core --save

Add these lines at the end of package.json:

,
  "browserify": {
    "transform": [
      [
        "babelify",
        {
          "presets": [
            "@babel/preset-env"
          ]
        }
      ]
    ]
  }

Regards

1 Like