npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dyimport

v1.0.2

Published

Library for dynamically importing modules using JavaScript ES modules.

Downloads

10

Readme

Dynamic Import Library

dyimport

Package-Version License ESMJS

dyimport is a library to dynamically import modules from a specific root directory with additional features such as caching and hashing to improve efficiency. This library is designed to make it easier to manage modules in large projects with complex folder structures.

Main feature

  • Dynamic Import: Imports modules based on filename at runtime.

  • Caching: Saves module import results to avoid unnecessary repeated imports and speed up module access.

  • Hashing: Using hashing to generate unique tokens that help in the caching process.

  • Security: Secure module import results using hashing and authtoken.

  • Error Handling: Error handling that provides error information during testing.

  • File Search: Searches files in directories and subdirectories recursively.

  • Global Folders: Can define multiple global folders at once in package.json using a root variable with array data type.

Installation

You can install dyimport via npm with the following command:

npm install dyimport

Use

Here's an example of how to use dyimport in your application:

Package.json configuration

To define a global root directory and allow dyimport to search for specific files in subfolders, you need to add the following configuration in your package.json:

{
  "name": "module-name",
  "version" : "x.x.x",
  "main": "index.js",
  "type": "module",
  "root": [
    "main",
    "path/to/your/modules"
  ]
}

Importing Modules

import load from 'dyimport';

// Import the module dynamically and search for 'module.js' in the root directory specified in package.json.
const modules = await load('module.js'); // importing multiple modules in one class.
const { module1, module2 } await load(module.mjs) // import multiple modules by function.

Using Imported Modules

After importing a module, you can use functions or objects exported from that module:

import load from 'dyimport';

async function runTest() {
  try {
    // Importing file 'path/to/you/modules/testFile.js'
    const { test1, test2 } = await load('testFile.js');
    
    // Using imported modules
    console.log(test1()); // Output: this is a test using dyimport.
    console.log(test2()); // Output: test successful.
  } catch (error) {
    console.error('Error:', error);
  }
}

runTest();

You can also do it like this:

import load from 'dyimport';
// Importing file 'path/to/you/modules/testFile.js'
const { test1, test2 } = await load('testFile.js');

async function runTest() {
  try {
    // Using imported modules
    console.log(test1()); // Output: this is a test using dyimport.
    console.log(test2()); // Output: test successful.
  } catch (error) {
    console.error('Error:', error);
  }
}

runTest();

Licence

This project is licensed under the MIT license. See LICENSE for more information.

Contact

For questions or further information, you can contact us at Github.