Call my NPM package (Node module) without adding the .js

1

Hi, I'm programming a module for nodejs that is installed through npm , everything works great, the code, the installation, everything as it has to be etc ..., my concern is that to use other modules it is not necessary to add the .js when doing require for example:

cons paquete = require('paquete')

instead of mine, if I do not put .js to the name of the package, it tells me that it can not be found, so I have to call it like this:

cons mipaquete = require('mipaquete.js')

Any idea why this happens and how can I solve it?

    
asked by Cristofer Fuentes 24.11.2016 в 23:14
source

2 answers

4

Using const paquete = require('paquete') nodejs will search hierarchically in:

  • ./node_modules/paquete.js
  • ./node_modules/paquete/index.js
  • ./node_modules/paquete/package.json
  • First try to locate the file as a .js in the node_modules folder (if the path is relative), if it does not find it, it will look for a directory with the name of the package and inside it will locate the file index.js , if is not successful will finally find, in that same directory, the entry main in the file package.json .

        
    answered by 24.11.2016 / 23:41
    source
    2

    When inside a file you do:

    require('./algo')
    

    Node will search in directorio algo for a file index.js , if you can not find it, search for index.json , if you do not search for index.node and if you do not find any of the above, then obviously launch a error. DOCUMENTATION

        
    answered by 24.11.2016 в 23:34