Helmut K.
Helmut K.

Reputation: 121

Using own Node.js module

I am trying to create my own module in node.js that I can use later.

For learning purpose I have created a javaScript class to practice this kind of stuff unfortunately I have no idea how to use it properly.

My class:

class Taschenrechner {
 let plus = (param1, param2) => {
   return param1 + param2;
  }

 let minus = (param1, param2) => {
   return param1 - param2;
  }  

 let mal = (param1, param2) => {
   return param1 * param2;
  }

 let geteilt = (param1, param2) => {
   return param1 / param2;
  }

 let potenz = (param1, param2) => {
   return param1 ** param2;
  }
}

So let just say I want to export all these functions so that I can use them like this:

let calculator = require("taschenrechner");
calculator.add(4, 4);

Something like that... (I'm still a rookie as you can see :D)

The official node modules use something like .exports is this something I could/should use for my own code as well?

Upvotes: 0

Views: 93

Answers (3)

Estus Flask
Estus Flask

Reputation: 223054

let isn't allowed inside class. With Babel and ES.next class fields it could be:

class Taschenrechner {
  static plus = (param1, param2) => {
   return param1 + param2;
  }
  ...
}

In ES6, it could be:

class Taschenrechner {}
Taschenrechner.plus = (param1, param2) => {
  return param1 + param2;
}
...

Taschenrechner acts only as a namespace here. Taschenrechner shouldn't be a class because it doesn't use a state or other class traits it could benefit from.

Modules are supposed to act as namespaces in JS. With CommonJS modules it should be:

exports.plus = (param1, param2) => {
  return param1 + param2;
};
...

With ES modules:

export const plus = (param1, param2) => {
  return param1 + param2;
};
...

Upvotes: 5

Kaloyan Kosev
Kaloyan Kosev

Reputation: 13067

When you require a module just by its name (require("taschenrechner")), NodeJS searches the module in node_modules directory. Therefore, this is a mechanism for importing NodeJS packages (libraries), private or public, published in the npm registry.

If you want to do so, see the How to Publish & Update a Package in the NPM docs. In the section How to Create Node.js Modules is explained how to define your module.

If you don't want to publish your package, you can always use the .exports mechanism to require your module. Just write a path in front of the name of the module (file). If the file is in the same directory, example: require("./taschenrechner")

Upvotes: 2

David784
David784

Reputation: 7464

If you want to use class, you wouldn't use let inside of it. That's not how to define a class. However I don't think you really want to use class anyway, because you have to instantiate a class, and there's no need here. Better to just define an object instead.

And yes, you'd just use module.exports to make your module. Like this:

const Taschenrechner = {
  plus: (param1, param2) => {
    return param1 + param2;
  },
  minus: (param1, param2) => {
    return param1 - param2;
  }
};

module.exports = Taschenrechner;

However for loading your module: unless you're actually loading your module through npm, you have to include some sort of path. Like this:

let calculator = require("./taschenrechner");
calculator.plus(4, 4);

Notice, you don't need the .js, that's assumed. but you need to have something like the ./ (that's if it's in the same directory as the script that's calling it). or some equivalent relative or absolute path.

Upvotes: 3

Related Questions