首页 > 解决方案 > 如何将 javascript 模块导入 Firefox WebExtension 后台脚本

问题描述

这是我的清单:

  "background": {
    "page": "./src/background_scripts/background.html"
  },

这是我的背景.html:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
        </head>
        <body>
<script type="module" src="./ScrapingContent.js"></script>
    
        </body>
    </html>

这是 ScrapingContent.js:

import * as cheerio from "../../node_modules/cheerio/index.js"

但我得到了错误:

ReferenceError: require is not defined

现在显然我还需要导入 require,所以我将我的 background.html 更改为:

        <!DOCTYPE html>
        <html>
            <head>
                <meta charset="UTF-8">
            </head>
            <body>
<script type="text/javascript"src="../../node_modules/requirejs/require.js"></script>
    <script type="module" src="./ScrapingContent.js"></script>
        
            </body>
        </html>

我得到了错误:

Error: Module name "lib/static" has not been loaded yet for context: _. Use require([])
https://requirejs.org/docs/errors.html#notloaded

如何导入这些模块/外部库?

标签: javascriptnode-modulesfirefox-addon-webextensionsbrowser-extension

解决方案


推荐阅读