gpt4 book ai didi

javascript - 如何在js中使用require函数

转载 作者:行者123 更新时间:2023-11-29 16:06:14 24 4
gpt4 key购买 nike

我在输入完成后得到正确的信用卡信息我调用了一个函数来使用 luhn 模块(npm install luhn)验证信用卡,因为我使用:

var luhn = require("luhn");
is_valid = luhn.validate(card); // should respond true.
if (!is_valid) {
console.log("Not a valid credit card");
}
return;`

Uncaught ReferenceError: require is not defined

很抱歉,如果这是一个简单的问题,但因为我找不到 npm 打包使用的逻辑简短解决方案。 onsubmit 我这次调用了kkTahsil()函数。

function kkTahsil() {
datalariAl();

var Iyzipay = require('iyzipay');
var iyzipay = new window.Iyzipay({
apiKey: 'sandbox-PZ8jicWrEeE1rt1O75FTOegr5lsW3xxx',
secretKey: 'sandbox-2Q6aaP1FK3HFrXkTsHfftxfiudFMfxxx',
uri: 'https://sandbox-api.iyzipay.com'
});

var nameOnCard = document.getElementById('name-on-card').value;
var expireMonth = document.getElementById('card-exp-month').value;
var expireYear = document.getElementById('card-exp-year').value;
var cvc= document.getElementById('card-cvv').value;

同样的错误。

所以在 js 中,必须有简单的方法来使用 npm 模块。但我还没有找到。我需要帮助。

最佳答案

require 在浏览器中不可用。它在 Node.js 中使用。

如果你想在客户端使用require 然后使用Browserify :

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

事实上,require 无法以 Node 中实现的形式在浏览器中使用。 require 的问题在于它是同步的。当你可以阻塞 I/O 时,它在事件循环的第一个 tick 时在服务器端工作,因为还没有绑定(bind)事件监听器,但它不会在浏览器中正常工作,因为它必须阻塞 UI下载、编译和运行模块的整个时间。

事实上,同步与异步模块加载一直存在争议。有关详细信息,请参阅这些答案:

关于javascript - 如何在js中使用require函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41315987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com