Editor.JS 语法错误:无法在模块外部使用 import 语句

问题描述 投票:0回答:1

我正在尝试将 editor.js 安装到我的项目中,但是,我不断遇到错误

Uncaught SyntaxError: Cannot use import statement outside a module

我可以通过 CDN 加载它,但由于某种原因,这无法工作。

以下是我迄今为止完成的故障排除步骤:

  1. "type":"module"
    添加到 JSON 文件
  2. type="module"
    添加到普通 js 脚本
  3. 将节点版本回滚到 v11.4.0(这是通过 stackoverflow 从另一个页面推荐的)。

我已经通过 stackoverflow 进行了搜索,但找不到除这些之外的解决方案。有什么建议吗?

HTML 文件:

<!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 文件:

import EditorJS from '@editorjs/editorjs';

const editor = new EditorJS();

打包 JSON 文件:

{
  "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"
  }
}

javascript node.js syntax-error package.json editorjs
1个回答
0
投票

如果您从 CDN 获取 editor.js,则不需要 import 语句。在你的 JS 文件中你需要的是:

const editor = new EditorJS();

我自己也摔倒了。我认为 Editor.js 文档here具有误导性。它首先说有 3 种安装 editor.js 的方法,但接下来的所有示例都假设您已经使用了

npm

© www.soinside.com 2019 - 2024. All rights reserved.