how to use require function in js

后端 未结 1 630
执念已碎
执念已碎 2020-12-17 06:13

I get properly credit card info upon input done I called a function to validate credit card with luhn module ( npm install luhn) as I use :

var luhn = requir         


        
1条回答
  •  执笔经年
    2020-12-17 06:52

    require is not available in the browser. It is used in Node.js.

    If you want to use require on the client side then use Browserify:

    Browserify lets you require('modules') in the browser by bundling up all of your dependencies.

    In fact, require couldn't be available in the browser in the form as it is implemented in Node. The problem with require is that it is synchronous. It works on the server side on the first tick of the event loop when you can block on I/O because no event listeners are bound yet, but it will not work in the browser without problems because it would have to block the UI for the entire time that the modules are downloaded, compiled and run.

    In fact synchronous vs asynchronous module loading has been a matter of controversy. See those answers for more details:

    • Exporting Node module from promise result
    • javascript - Why is there a spec for sync and async modules?

    0 讨论(0)
提交回复
热议问题