0
-- plugins
---- myplugin1
------ core
---- myplugin2
------ core

If this is my directory structure, is there any way for me to import all core folders from plugins without knowing the name myplugin1 etc?

require('/plugins/./core')

I know how to require from parent folders... but there seem to be nothing about child folders?

P. Nick
  • 955
  • 1
  • 12
  • 33
  • Read the directory `plugins`, for each file, check if it is a directory, read it and check if it has `core` directory. If `true` require. It is an easy to do script. – Abhishek Gupta Apr 12 '18 at 12:06

2 Answers2

1

Node-Cheat available here, run node app followed by npm i glob.

Possible try:

const glob = require('glob');
const path = require('path');

glob.sync('./plugins/**/core/*.js').forEach(( file ) => {
   require(path.resolve( file ) );
});

Expected output:

myplugin1 core loading
myplugin2 core loading
Zeeshan Hassan Memon
  • 8,105
  • 4
  • 43
  • 57
0

You can use require-dir to achieve this.

These will be the steps

npm install require-file-directory

var requireDir = require('require-dir');
var dir = requireDir('pathToyourCoreDirectory');

And inside the above handler function you can require all of the modules

Muhammad Usman
  • 10,039
  • 22
  • 39
  • I don't see how this solves my problem. It still looks like I have to specify the name of the plugin folders. Care to explain into detail? The npmjs site doesn't really help me – P. Nick Apr 12 '18 at 14:43