【问题标题】:How to use import statement, it is outside the module at the moment如何使用import语句,目前在模块之外
【发布时间】:2021-06-05 14:03:37
【问题描述】:

我的 html 文件中有这个脚本:

import { createRequire } from 'module';
const require = createRequire(import.meta.url);
// can now use `require` in an ESM

我收到以下错误: 未捕获的语法错误:无法在模块外使用 import 语句

在我的代码后面:

// Set options as a parameter, environment variable, or rc file.
        require = require("esm")(module/*, options*/)
        module.exports = require("./main.js")

我这样做是为了以后能够运行这一行:

const io = require("socket.io-client");

【问题讨论】:

  • 有一个很好的理由,你必须越过障碍才能使这个 lib 在 Node.js 中工作。它适用于浏览器。 我的 html 文件中有这个脚本 - 为什么会有它? 'module' 是 Node 内置的。

标签: html node.js import socket.io require


【解决方案1】:

已解决。我必须在脚本标签中添加“模块”。

<script type="module"     
require = require("esm")(module/*, options*/)
module.exports = require("./main.js")     
const io = require("socket.io-client");
></script>

【讨论】:

    猜你喜欢
    • 2020-10-08
    • 2022-01-25
    • 2020-02-09
    • 2021-01-30
    • 2021-08-24
    • 2021-05-18
    • 2020-08-25
    • 2020-01-27
    相关资源
    最近更新 更多