Importing nondescript files with NodeJS

3

I am creating a file structure in an application where each directory contains an "index" file. This index file takes the name of my directory to improve readability. Ex:

foo
|--foo.js    // <- index
|--bin.html
|--bar
|  |--bar.js // <- index
   |--bin.html

With this, the structure that will be used for a require() when importing any file will always be something like

const foo = require('./foo/foo');
const bar = require('./foo/bar/bar');

I look for some way to create a file without the nomenclature, and that the NodeJS understands for the import, would be something like:

foo
|--.js // <- sem nome = arquivo da pasta
|--bin.html

So I could work with files similar to namespace's ...

Is there any way to do this with NodeJS?

    
asked by anonymous 11.01.2017 / 15:41

1 answer

1

You can use foo/index.js and require('./foo') (this is more prudent and very common). Or you can add package.json to the foo directory and give any name to main .

eg.

foo / package.json

{"main": "qualquer-coisa.js"}

main.js

require('./foo') // vai dar require em './foo/qualquer-coisa.js'

The complete algorithm is in: link

But does not do that , it does not go far with the conventions.

Better to just do

foo
|--index.js
|--bin.html
require('./foo')
    
25.04.2017 / 21:11