简体   繁体   English

我的 webpack 捆绑脚本中未定义的节点模块

[英]Node modules undefined in my webpack bundled script

I am making a web app which, based on a jped picture, recognizes characters and renders it in an interactive interface for the user - this includes some async code.我正在制作一个 web 应用程序,它基于 jped 图片识别字符并将其呈现在用户的交互式界面中 - 这包括一些异步代码。 There are 4 js scripts file, which all require npm modules, and an html view.有4个js脚本文件,都需要npm个模块,html个视图。

In order to test the app client-side, I have decided to bundle the scripts together.为了测试应用程序客户端,我决定将脚本捆绑在一起。 It shows the following error message:它显示以下错误消息:

Uncaught ReferenceError: require is not defined未捕获的 ReferenceError:未定义要求

List of my npm modules whose code returns this error at run time:我的 npm 模块列表,其代码在运行时返回此错误:

I have tried:我努力了:

  1. browserify my scripts into a bundle, but I read that it would not work with async functions;我的脚本浏览器打包成一个包,但我读到它不适用于异步函数;
  2. webpack the scripts into a bundle, but Node modules like fs and child_process are returning 'undefined'; webpack将脚本打包,但像 fs 和child_process这样的节点模块返回“未定义”;
  3. adding a specific Node module, child-process-ctor , to force child_process to the included添加一个特定的 Node 模块, child-process-ctor ,强制 child_process 到包含

Alas, the same error message is returned. las,返回相同的错误消息。

Questions:问题:

  1. Is bundling the scripts the right approach?捆绑脚本是正确的方法吗?
  2. Is the problem that webpack does not transpile fs and child_process correctly ?问题是 webpack 没有正确转译 fs 和 child_process 吗?
  3. Which possible solutions should I consider?我应该考虑哪些可能的解决方案?

Thanks all.谢谢大家。 This is my 1st question on SO -- any feedback is much welcome!这是我关于 SO 的第一个问题——非常欢迎任何反馈!

PS: This might be redundant with Using module "child_process" without Webpack . PS:这可能与Using module "child_process" without Webpack是多余的。

Okay this answer is a follow up to my comments, which answer the question more directly.好吧,这个答案是我评论的后续,它更直接地回答了问题。 However here I'll go into more detail than is probably necessary, but it will thoroughly answer what you asked.然而,在这里我将 go 比可能需要的更详细,但它会彻底回答你的问题。 Plus it's educational and I'd say it's pretty fun once you start really digging into it:D此外,它具有教育意义,一旦你开始真正深入研究它,我会说它非常有趣:D

To start at the beginning.从头开始。 As the inte.net in its early days became more advanced the need for a type of "front end logic" increased and Netscape's response to this demand was to birth a competitive, cutting edge programming language in record time.随着早期的 inte.net 变得更加先进,对“前端逻辑”类型的需求也在增加,而 Netscape 对这种需求的回应是在创纪录的时间内诞生了一种具有竞争力的尖端编程语言。

And by record time I mean 10 days, and by competitive I mean barely functional.创纪录的时间是指 10 天,而竞争是指几乎没有功能。

That's right Javascript was born in 10 days ( literally ).没错 Javascript 在 10 天内出生( 字面意思)。 As you can imagine it was a pretty poor language, but it worked well enough that people started using it.你可以想象这是一种非常糟糕的语言,但它运行良好以至于人们开始使用它。

Because it was the programming language of the inte.net, and because of how fast the inte.net grew, enough people started to use it that the thought of removing it became scary.因为它是 inte.net 的编程语言,并且由于 inte.net 的发展速度如此之快,已经有足够多的人开始使用它,以至于删除它的想法变得可怕。

If you changed it you would destroy backward compatibility with millions of websites.如果你改变它,你会破坏与数百万网站的向后兼容性。 The other idea would be to keep it, but also implement a new standard.另一个想法是保留它,但也实施新标准。 However it would be hard to justify this because javascript already took a lot of work to upkeep, upkeeping multiple standards would be a nightmare (cough... flash cough).然而,很难证明这一点是合理的,因为 javascript 已经花了很多工作来维护,维护多个标准将是一场噩梦(咳嗽...... flash咳嗽)。

Javascipt was easy enough for "new" programmers to learn, but the problem was javascript's only 1 language in a world where php, ruby, mySql, Mongo, Css, Html all rule as dominant kings in their respective kingdoms. Javascipt 对于“新”程序员来说很容易学习,但问题是在 php、ruby、mySql、Mongo、Css、Html 都统治着各自王国的世界中,javascript 是唯一一种语言。

So someone thought it was a good idea to move javascript to the server and thus node.js was born.于是有人认为把javascript移到服务器上是个好主意,于是node.js诞生了。

However for javascript to mean anything on the server it had to be able to do things that you wouldn't want it to be able to do in your browser.但是,要使 javascript 在服务器上具有任何意义,它必须能够执行您不希望它在浏览器中执行的操作。 For example, scan your hard drive and edit your files.例如,扫描您的硬盘并编辑您的文件。

If every website you visit could start scanning and uploading everything in your system well....如果您访问的每个网站都可以开始扫描并上传您系统中的所有内容......

However if your server software can't edit or read files you need it to well....但是,如果您的服务器软件无法编辑或读取文件,您就需要它……

You get the idea.你明白了。 It's the same language, but because of security issues node.js has some differences.它是同一种语言,但由于安全问题 node.js 有一些差异。 Mainly the modules it's allowed to use.主要是允许使用的模块。

Now for the fun part.现在是有趣的部分。 Can you run node.js client side in a browser.你能在浏览器中运行 node.js 客户端吗? Technically yes.技术上是的。 In fact now that we're dumping entire operating systems into a subset of javascript called asm.js there really isn't anything javascript can't do with enough processing power.事实上,现在我们将整个操作系统转储到 8822996504788 的子集asm.js中,如果有足够的处理能力,8822996504788 真的没有什么做不到的。

However even if you dump the entire node.js engine (which is basically a stripped down version of chrome) into asm.js you would still have the same security limitations placed by the "Host" browser and so your modules could only run within the sandbox the browser provides.但是,即使您将整个 node.js 引擎(基本上是 chrome 的精简版)转储到 asm.js 中,您仍然会受到“主机”浏览器设置的相同安全限制,因此您的模块只能在沙箱中运行浏览器提供。

So it is technically just a browser within another browser Running at half the speed with the same security limitations.因此,从技术上讲,它只是另一个浏览器中的一个浏览器,以一半的速度运行,但具有相同的安全限制。

Is it something I would recommend doing?这是我建议做的事情吗? Of course not.当然不是。

Is it something that people haven't already tried before?这是人们以前没有尝试过的东西吗? Of course not.当然不是。

So I hope that helps answer your question.所以我希望这有助于回答你的问题。

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

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