radjiv
radjiv

Reputation: 119

how to pass a string/object in a require

I want to pass a string in javascript module

module1.js

  var currency = 'dollar'
  module = require('./module2')(currency)

How can I do that because right now I have this error

TypeError: string is not a function e=[TypeError:string is not a function]

Thanks you all

Upvotes: 0

Views: 368

Answers (3)

Kavya Mugali
Kavya Mugali

Reputation: 1108

If you want to pass currency as a parameter to a function in module2 use:

module = require('./module2').FunctionName(currency);

If you want to access a property by name currency ('dollar' property in this case) in module2 use:

module = require('./module2')[currency];

Upvotes: 0

uzaif
uzaif

Reputation: 3531

I think You have to build function inside module and than call it after require it, it is better solution for your problem change.js

module.exports = {
  escape: function(html) {
    return String(html)
      .replace(/&/g, '&')
      .replace(/"/g, '"')
      .replace(/'/g, ''')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;');
  }

call it this way

var change= require("change");
change.escape(yourElement);

Upvotes: 0

omarjmh
omarjmh

Reputation: 13888

 var currency = 'dollar'
 module = require('./module2').yourFunction(currency);

Upvotes: 1

Related Questions