Editor.JS SyntaxError: Cannot use import statement outside a module

552 Views Asked by At

I am trying to install editor.js into my project, however, I keep running into the error Uncaught SyntaxError: Cannot use import statement outside a module.

I'm able to load it through there CDN but for some reason this is not able to work.

Here are the troubleshooting steps I've done so far:

  1. Add "type":"module" to JSON file
  2. Add type="module" to vanilla js script
  3. rollback version of node to v11.4.0 (this was recommended from another page through stackoverflow).

I've searched through stackoverflow and I'm not able to find a solution other then these. Any advice?

HTML File:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <div  id="editorjs"></div>

    <script src="index.js"></script>

  </body>
</html>

JS File:

import EditorJS from '@editorjs/editorjs';

const editor = new EditorJS();

package JSON File:

{
  "name": "editor",
  "version": "1.0.0",
  "type": "module",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@editorjs/editorjs": "^2.18.0"
  }
}

enter image description here

1

There are 1 best solutions below

0
On

You don't need the import statement if you are getting editor.js from a CDN. In your JS file all you need is:

const editor = new EditorJS();

I fell over this myself. I think the Editor.js documentation here is misleading. It starts by saying there are 3 ways to install editor.js, but then all the remaining examples assume you've used npm.