3

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.

asked Dec 24, 2016 at 18:45
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? 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 ? 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. Commented Dec 24, 2016 at 18:57
  • I am client side js 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. Commented Dec 24, 2016 at 18:59

1 Answer 1

16

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:

answered Dec 24, 2016 at 21:59

2 Comments

Hi, Thank you for the help, But now I have come another point. I use a credit card form at html between form tag. And my server is at firebase. I have a start.js which is sending data to github.com/iyzico/iyzipay-node server. start.js same like above code (2.part) without function And when I start it with npm run-script start. Its working (sendind record to credit card charge to) But I could not find the way how to run this code after submit form. Hope clear. I will be very happy if you could help for this. :)
You should start a separate question for this with a relevant description, what you tested, code, etc. - Feel free to start the question (if you still need help), and tag me in it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.