GithubHelp home page GithubHelp logo

import-modules's Introduction

import-modules

Import all modules in a directory

This package is intentionally simple. Not interested in more features.

Install

$ npm install import-modules

Usage

.
└── directory
    ├── foo-bar.js
    └── baz-faz.js
const importModules = require('import-modules');

const modules = importModules('directory');

console.log(modules);
//=> {fooBar: [Function], bazFaz: [Function]}

API

importModules(directory?, options?)

directory

Type: string
Default: __dirname

Directory to import modules from. Unless you've set the fileExtensions option, that means any .js, .json, .node files, in that order. Does not recurse. Ignores the caller file and files starting with . or _.

options

Type: object

camelize

Type: boolean
Default: true

Convert dash-style names (foo-bar) and snake-style names (foo_bar) to camel-case (fooBar).

fileExtensions

Type: string[]
Default: ['.js', '.json', '.node']

File extensions to look for. Order matters.

Related

import-modules's People

Contributors

adoyle-h avatar coreyfarrell avatar joelasaur avatar jopemachine avatar richienb avatar sindresorhus avatar swernerx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

import-modules's Issues

Breaks in repl

> const importModules = require('import-modules');
Thrown:
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
    at validateString (internal/validators.js:125:11)
    at Object.dirname (path.js:1121:5)
    at Object.<anonymous> (/Users/xo/code/unraid/cli/node_modules/import-modules/index.js:8:24)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)

TypeScript support

This is an awesome project, and it would be great to get TypeScript support! 😊

Cannot read property 'filename' of undefined

I have the following index.js file:

import importModules from 'import-modules'

const modules = importModules('./test')

console.log(modules);

export default modules

In the same folder as the index.js file, there is a sub-folder test with multiple .js files.

But, the above code produces the following exception: "Cannot read property 'filename' of undefined"

Jest modifies require.extensions

I'm writing a shareable ESLint config that uses eslint-plugin-unicorn which uses this module.

Inspired by Test Your ESLint config I'm testing my config with Jest.

I was seeing failures around being able to load the eslint-plugin-unicorn rules and after looking at the code here and looking across tools I was using the problem I'm experiencing seems to be caused by jestjs/jest#281.

While the problem is "caused by" Jest it seems like a difficult issue for Jest to resolve and I wondered if a solution might be possible in import-modules, e.g.

var extensions = Object.assign({}, {
    // Jest will redefine require.extensions to be {}
    // so add back default implementation(s) if not available
    '.js': require('default-require-extensions/js')
  }, require.extensions);

for (const ext of Object.keys(extensions)) {
  ...

A change like the above would mean that I get the default js implementation back and rules for eslint-plugin-unicorn load.

This is pretty similar to #1, but continues to use require.extensions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.