Using Browserify with JQuery - what does it 'really' mean?

做~自己de王妃 提交于 2019-12-05 23:50:11

Node.js and jQuery:

One important distinction between Node.js and a browser is that Node.js is just a:

platform built on Chrome's JavaScript runtime

it simply means that it allows you to execute javascript code. Browsers also have their own JS runtime to execute scripts on the client side and in addition provide a mean "for representing and interacting with objects in HTML, XHTML, and XML documents." and that is the Document Object Model (DOM).

In Node.js there are no HTML files and you just have to do with JS code, thus using jQuery in Node.js doesn't make any sense, since jQuery:

makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.


Node.js and browserify:

Node.js provides a module loading system which allows you to include other modules using the require keyword. So any JS code containing the require code cannot be executed in the browser, since up to ECMA5 there are no built-in module loading mechanisms.

Browserify simply mocks the require keyword and allows you to make use of it also in browsers, as explained here:

Browserify uses the term entry file(s) to describe where it will start reading a dependency graph, and its output is referred to as a bundle.


Node.js for your project?:

Since your project is aimed to be run inside a browser (and not on a server) there is no need to migrate to Node.js. However, you could use Nodejs to better maintain your project:

  • Seperate the project in modules in development and create a single bundle file with browserify for production.
  • Use a number of preprocessors, and compiles (e.g. coffeeScript, Less, etc)
  • Test your modules (e.g mocha, jest)
  • Use a build system (e.g gulp)
  • etc...

And after you have, tested your modules (and compiled your coffeescript!) you just let browserify to created your main.bundle.js and just import it like this in your production:

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="main.bundle.js"></script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!