gpt4 book ai didi

javascript - 如何在 HTML 文件中包含 npm 包?

转载 作者:行者123 更新时间:2023-12-01 08:31:51 25 4
gpt4 key购买 nike

我的 HTML 文件:

<!DOCTYPE html>
<head>
...
</head>
<body>
...
<script src="script.js"></script>
</body>

我的 JavaScript 文件 - script.js:

import * as FilePond from 'filepond';

const myPond = FilePond.create({
multiple: false,
name: 'filepond'
});

document.body.appendChild(pond.element);

但是出现了错误,浏览器说:

Uncaught SyntaxError: Cannot use import statement outside a module

所以我将 script.js 编辑成这样:

const FilePon = require('filepond')

const myPond = FilePond.create({
multiple: false,
name: 'filepond'
});

document.body.appendChild(pond.element);

但是又报错了,浏览器说:

Uncaught ReferenceError: require is not defined at script.js:1

我该如何解决?

最佳答案

很简单:

  1. 只需包含来自 CDN 的 FilePond css 和 js 文件,而不是像:

    <link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet">
    <script src="https://unpkg.com/filepond/dist/filepond.js"></script>
  2. 接下来,您不需要任何导入或要求。您可以简单地使用其余代码,因为 FilePond 现在全局声明如下:

const myPond = FilePond.create({
multiple: false,
name: 'filepond'
});

document.body.appendChild(myPond.element);
<link href="https://unpkg.com/filepond/dist/filepond.css" rel="stylesheet">
<script src="https://unpkg.com/filepond/dist/filepond.js"></script>

关于javascript - 如何在 HTML 文件中包含 npm 包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61753463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com