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.

Cappittall
  • 3,300
  • 3
  • 15
  • 23
  • 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 Dec 24 '16 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 Dec 24 '16 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 Dec 24 '16 at 18:57
  • I am client side js – Cappittall Dec 24 '16 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 Dec 24 '16 at 18:59
  • Ok. you are right at all, but if you could provide a sample which can work for client side to work above 'luhn' check only. I will be appreciated very much if it is in possibility. – Cappittall Dec 24 '16 at 19:19
  • On the client side, Javascript is loaded into web pages with ` – jfriend00 Dec 24 '16 at 19:46
  • Did [my answer](https://stackoverflow.com/questions/41315987/how-to-use-require-function-in-js/41317129#41317129) below help you? Any comments? – rsp Dec 27 '16 at 18:18

1 Answers1

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:

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
  • 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 https://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. :) – Cappittall Dec 29 '16 at 09:36
  • 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. – sramzan May 10 '17 at 01:25