简体   繁体   English

参考错误:找不到变量:在导入节点模块时需要<script> tag

[英]ReferenceError: Can't find variable: require when importing Node module in <script> tag

Having trouble importing a Node Module using the <script> tag from the HTML.使用 HTML 中的<script>标记导入节点模块时遇到问题。

Folder structure:文件夹结构:

.
├── dist/home.html
├── src/html/home.html
└── node_modules
    └── @group
        ├── sub1/...
        └── sub2/...
            └── src/MyModule.js

The module, "MyModule.js", uses other sub-packages (eg sub1) by using "require" like so:模块“MyModule.js”通过使用“require”来使用其他子包(例如 sub1),如下所示:

const sub1 = require('@group/sub1/x')
...
class MyModule extends XX { ... }
module.exports = ...

I tried to import it manually like this:我尝试像这样手动导入它:

<script type="module" src="/node_modules/@group/sub2/src/MyModule.js"></script>

But it results in ReferenceError: Can't find variable: require .但它导致ReferenceError: Can't find variable: require

Is there a solution for this?有解决方案吗?

FYI :仅供参考

  • Gulp is used to build the /dist folder, but an error currently prevents compiling JS files. Gulp 用于构建/dist文件夹,但当前错误阻止编译 JS 文件。 Using <script> is a temporary workaround.使用<script>是一种临时解决方法。
  • The module comes with an ES2015 precompiled version ( @group/sub2/dist/prebuilt.js ), if it helps.如果有帮助,该模块带有 ES2015 预编译版本 ( @group/sub2/dist/prebuilt.js )。

I'm able to reproduce the error in the browser's console:我能够在浏览器的控制台中重现错误:

code代码

const sub1 = require('test')

error错误

Uncaught ReferenceError: require is not defined
    at <anonymous>:1:14

This error happens because require is only defined in node.发生此错误是因为 require 仅在 node.js 中定义。 https://nodejs.org/api/all.html#addons_loading_addons_using_require https://nodejs.org/api/all.html#addons_loading_addons_using_require

The filename extension of the compiled Addon binary is .node (as opposed to .dll or .so).已编译的 Addon 二进制文件的文件扩展名是 .node(与 .dll 或 .so 相反)。 The require() function is written to look for files with the .node file extension and initialize those as dynamically-linked libraries.编写 require() 函数以查找具有 .node 文件扩展名的文件并将其初始化为动态链接库。

When calling require(), the .node extension can usually be omitted and Node.js will still find and initialize the Addon.调用 require() 时,通常可以省略 .node 扩展名,Node.js 仍会找到并初始化插件。 One caveat, however, is that Node.js will first attempt to locate and load modules or JavaScript files that happen to share the same base name.然而,一个警告是 Node.js 将首先尝试定位和加载碰巧共享相同基本名称的模块或 JavaScript 文件。 For instance, if there is a file addon.js in the same directory as the binary addon.node, then require('addon') will give precedence to the addon.js file and load it instead.例如,如果在与二进制 addon.node 相同的目录中有一个文件 addon.js,那么 require('addon') 将优先加载 addon.js 文件并加载它。


Here is a general example of how to load an external module in a browser https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Standard_Import这是如何在浏览器中加载外部模块的一般示例https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Standard_Import

The module: file.js模块:file.js

function getJSON(url, callback) {
  let xhr = new XMLHttpRequest();
  xhr.onload = function () { 
    callback(this.responseText) 
  };
  xhr.open('GET', url, true);
  xhr.send();
}

export function getUsefulContents(url, callback) {
  getJSON(url, data => callback(JSON.parse(data)));
}

The main program: main.js主程序:main.js

import { getUsefulContents } from '/modules/file.js';

getUsefulContents('http://www.example.com',
    data => { doSomethingUseful(data); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM