Hi Matias,

It's mostly a question of definition:

A *module* is a file (see the official documentation 
<https://nodejs.org/api/modules.html#modules_modules>). 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 
   <https://docs.npmjs.com/getting-started/publishing-npm-packages>.

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".

Le lundi 6 août 2018 03:32:09 UTC+2, Matias Salimbene a écrit :

> 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!
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/692cb737-0397-404b-ac0b-fc171df5c8e4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to