Conceptual question about modules [learnyounode: Make It Modular]

45 views
Skip to first unread message

Matias Salimbene

unread,
Aug 5, 2018, 9:32:09 PM8/5/18
to nodejs

Hello there, I'm going through the learnyounode workshop, currently on the Make It Modular section. I'm not getting a clear picture of how modules work.


Suppose I want to create a new module named "mod1":

  • I would have to create a subfolder named "mod1" with files index.js and package.json. Does index.js contains the actual code, and package.json is simply descriptive?

  • The workshop doesn't seem to use said structure but instead simple uses a separate file for the exported function code. I think it has to do with using a single function export, rather that a full module that would contain any amount of function (I guess).

  • Even though I've created the subfolder with both files, when i ran the following from cli:

node -pe "require('mod1')"

I get

Error: Cannot find module 'mod1' error.

Any clarification to these is greatly appreciated, cheers!

Simon Renoult

unread,
Aug 7, 2018, 12:35:22 PM8/7/18
to nodejs
Hi Matias,

It's mostly a question of definition:

A module is a file (see the official documentation). A module (ie: a file) can export anything from a variable to a function. If it starts with "./" (ie: "require('./foo')") Node.js will try to find it based on its relative path. If it's just the package name (ie: "require('path')"), Node.js will try to find it in its native modules (http, fs, etc.) or in your local installed modules (in "node_modules").

So if you want to create a new module named "mod1", you have two options:
  • Simple solution: create a file named "mod1.js". Don't forget to prepend "./" when requiring the file: "require('./mod1.js');". The file extension ".js" can be omitted.
  • Complicated solution: publish an npm package.
To answer your questions:

I would have to create a subfolder named "mod1" with files index.js and package.json. Does index.js contains the actual code, and package.json is simply descriptive?

No. This is only necessary when creating an npm package (aka the "Complicated solution").

The workshop doesn't seem to use said structure but instead simple uses a separate file for the exported function code. I think it has to do with using a single function export, rather that a full module that would contain any amount of function (I guess).

No. A module can contain as many functions as you want. And a module can be made of modules, so it scales. But it's mostly a matter of definition. In Node.js a module is just a file. You could consider that what you call module is what we usually call a pacakge. A package can be found on NPM (= Node Package Manager). A package is not an official Node.js term though. A package is a module (aka a JavaScript file) + a package.json file that describes this module dependencies and other meta information.

Even though I've created the subfolder with both files, when i ran the following from cli: 
node -pe "require('mod1')"

I get

Error: Cannot find module 'mod1' error.

Yep, you forgot "./" "before "mod1".
Reply all
Reply to author
Forward
0 new messages