Huge News!Announcing our $40M Series B led by Abstract Ventures.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

  • 3.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
29K
increased by22.73%
Maintainers
1
Weekly downloads
 
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 { directoryImport } = require('directory-import');

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

Simple usage

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

const { directoryImport } = require('directory-import');

const importedModules = directoryImport('../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 { directoryImport } = require('directory-import');

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

{Function} Callback:

PropertyTypeDescription
fileNameStringFile name
filePathStringFile path
fileDataStringExported file data
indexNumberThe module index

More examples

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

directoryImport();
Async call:
const { directoryImport } = require('directory-import');

const importedModules = directoryImport('../sample-directory', 'async');

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

directoryImport('../sample-directory', 'async', (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 { directoryImport } = require('directory-import');

const importedModules = directoryImport('../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);

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

Keywords

FAQs

Package last updated on 28 Jul 2023

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