最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to use require function in js - Stack Overflow

programmeradmin1浏览0评论

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 = 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

I am sorry If this is simple question but since I could not find a logic short solution for npm packed usage. onsubmit I call this time kkTahsil() function.

function kkTahsil() {
datalariAl();

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

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;

again same error.

so in js, there must be easy way to use npm modules. But I could not found yet. Please I need a help.

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 = 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

I am sorry If this is simple question but since I could not find a logic short solution for npm packed usage. onsubmit I call this time kkTahsil() function.

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;

again same error.

so in js, there must be easy way to use npm modules. But I could not found yet. Please I need a help.

Share Improve this question edited Mar 28, 2017 at 8:52 Cappittall asked Dec 24, 2016 at 18:45 CappittallCappittall 3,4413 gold badges18 silver badges25 bronze badges 8
  • What is the module system you're using? If you are using the keyword "require", then something is going to have to implement that (it's not a straight JavaScript thing). Are you in a node environment here or is this client-side javascript? – Tim Consolazio Commented Dec 24, 2016 at 18:48
  • I am really new at node and npm. So I found this 'luhn' module at npm page. and installed node module dir. with npm install command. At web or js there is no extra definition about module system. What should I use. and How ? – Cappittall Commented Dec 24, 2016 at 18:53
  • I guess I'd start with the node one, CommonJS, or RequireJS, which I know is also popular (it's not the native Node one, but it unifies the module system between server and client). I'll tell you one thing though, if I was brand new to all this, I probably wouldn't be trying to do what you're doing. I'd hit the "hello world" tutorials. – Tim Consolazio Commented Dec 24, 2016 at 18:57
  • I am client side js – Cappittall Commented Dec 24, 2016 at 18:58
  • Then you're really heading the wrong way. I recommend looking into ES6 modules, RequireJS, SystemJS, or some other client-side module system. Do the tutorials from scratch and you'll know why the above isn't working. – Tim Consolazio Commented Dec 24, 2016 at 18:59
 |  Show 3 more comments

1 Answer 1

Reset to default 15

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)

  1. 暂无评论