Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

als-require

Package Overview
Dependencies
Maintainers
0
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

als-require

A utility for using CommonJS require in the browser and creating bundles.

  • 0.1.0
  • npm
  • Socket score

Version published
Weekly downloads
25
increased by150%
Maintainers
0
Weekly downloads
 
Created
Source

als-require

als-require is a user-friendly utility designed to facilitate the use of the require function in web browsers and to create bundles for CommonJS modules. It simplifies the process of module management in browser environments, allowing for seamless integration and deployment of CommonJS-based code.

Features

  • Dynamic Module Loading: Load JavaScript modules dynamically based on runtime conditions.
  • Cyclic Dependency Detection: Automatically detects and handles cyclic dependencies within modules.
  • Error Handling: Provides detailed error information for debugging loading issues.
  • Flexibility: Supports various module formats and can be easily integrated into different JavaScript environments.

Installation

To install als-require, use npm:

npm install als-require

Overview of als-require

als-require includes two main components designed to enhance module handling in web browsers and server environments. It facilitates the use of CommonJS modules directly in the browser and supports the generation of module bundles for more efficient deployment.

Components

  1. Browser Script (require.js): This script is used in the browser to dynamically load and resolve modules. It allows for direct module usage without pre-bundling, enabling more flexible and dynamic web applications.

  2. Bundle Generator Script (index.js): This Node.js script is used to generate bundles of CommonJS modules, which can then be included in web projects. It simplifies the deployment process by compiling dependencies into a single file.

Usage Scenarios

als-require can be utilized in two primary ways:

1. Direct Browser Usage

In this scenario, the require.js script dynamically loads the modules directly in the browser. This is suitable for environments where modules need to be loaded on the fly without pre-compilation.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Dynamic Module Loading</title>
</head>
<body>
    <script src="node_modules/als-require/require.js"></script>
    <script>
       getModule('./module1/a.js')
       .then(exports => {
          window.test = exports.test();
       });
    </script>
</body>
</html>
2. Bundle Creation and Usage

In this scenario, als-require is used to generate a bundle that consolidates all the required modules into a single file. This bundle can then be used in the browser, reducing the number of HTTP requests and streamlining the module loading process.

Example for Generating a Bundle:

const fs = require('fs');
const getRequire = require('als-require');
const script = getRequire('./module1/a', 'test');
fs.writeFileSync('test.js', script);

This bundle can also be served directly from a server using frameworks like Express:

const content = getRequire('./module1/a', 'test');
app.get('/bundle.js', (req, res) => {
   res.send(content);
});

API Reference

getRequire(path, varName)

Loads a module from the specified path and assigns it to a variable named varName.

  • path: String - The relative path to the module file.
  • varName: String - The variable name to which the module's exports will be assigned.

Examples

Loading a Simple Module

const config = getRequire('./config', 'config');
console.log(config);

Handling Errors

try {
  const result = getRequire('./invalid/path', 'test');
} catch (error) {
  console.error('Failed to load module:', error.message);
}

Keywords

FAQs

Package last updated on 21 Jun 2024

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