New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

directory-import

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

directory-import

Module will allow you to synchronously or asynchronously import (requires) all modules from the folder you specify

  • 2.3.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Node version required GitHub code size Downloads Discord server

Install directory-import from npm

About

Module will allow you to synchronously or asynchronously import (requires) all modules from the folder you specify.

You can use modules from the returned object, or you can invoke function per file


Installation

npm i directory-import

After install, you can require module and import files:

const importDir = require('directory-import');

// Returns: { filePath1: fileData1, filePath2: fileData2, ... },
const importedModules = importDir({ directoryPath: './' });

Simple usage

This is one simple example of how to use the library and how it works under the hood:

const importDir = require('directory-import');

const importedModules = importDir({ directoryPath: '../sample-directory' });

console.info(importedModules);
GIF how it works under the hood

Path to directory from GIF above

You can invoke callback on each file

This can be useful when, for example, you need to do some action depending on the imported file.

const importDir = require('directory-import');

importDir({ directoryPath: '../sample-directory' }, (moduleName, modulePath, moduleData) => {
  console.info({ moduleName, modulePath, moduleData });
});
GIF how it works under the hood

Params

{Object} Options:

PropertyTypeDefault valueDescription
directoryPathString"./"Relative path to directory
importMethodString"sync"Import files synchronously, or asynchronously
includeSubdirectoriesBooleantrueIf false — files in subdirectories will not be imported
webpackBooleanfalseWebpack support. Example using
limitNumber0Indicates how many files to import. 0 - to disable the limit
excludeRegExpundefinedExclude files paths. Example

{Function} Callback:

PropertyTypeDescription
fileNameStringFile name
filePathStringFile path
fileDataStringExported file data

More examples

Minimum code to run modules that are in the same folder as the code below:
const importDir = require('directory-import');

importDir();
Async call:
const importDir = require('directory-import');

const importedModules = importDir({ importMethod: 'async', directoryPath: '../sample-directory' });

// Promise { <pending> }
console.info(importedModules);
Async call with callback:
const importDir = require('directory-import');

importDir({ importMethod: 'async', directoryPath: '../sample-directory' }, (moduleName, modulePath, moduleData) => {
  // {
  //   moduleName: 'sample-file-1',
  //   modulePath: '/sample-file-1.js',
  //   moduleData: 'This is first sampleFile'
  // }
  // ...
  console.info({ moduleName, modulePath, moduleData });
});
Put the result in a variable and invoke a callback for each module
const importDir = require('directory-import');

const importedModules = importDir({ directoryPath: '../sample-directory' }, (moduleName, modulePath, moduleData) => {
  // {
  //   moduleName: 'sample-file-1',
  //   modulePath: '/sample-file-1.js',
  //   moduleData: 'This is first sampleFile'
  // }
  // ...
  console.info({ moduleName, modulePath, moduleData });
});

// {
//   '/sample-file-1.js': 'This is first sampleFile',
//   ...
// }
console.info(importedModules);
Exclude .json extension
const importDir = require('directory-import');

const result = importDir({ directoryPath: '../sample-directory', exclude: /.json$/g });

console.info(result);
using with webpack
// You must specify the node_modules dir. Otherwise webpak will generate an error
const importDir = require('node_modules/directory-import');
// or const importDir = require('../node_modules/directory-import');

// You must specify the path from the root directory.
// And also indicate that we want to work with the webpack (webpack: true)
const result = importDir({ directoryPath: './sample-directory', webpack: true });

console.info(result);

Help

If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official Discord server.

Discord server

Although the server was created for Russian speakers, you can also write in English! We will understand you!

Keywords

FAQs

Package last updated on 08 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc