Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
als-require
Advanced tools
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.
To install als-require
, use npm:
npm install als-require
als-require
includes two main parts:
The browser's script called getModule
, is a function which fetching all module's chain and then laughing all the chain to get the result.
The fetch is async, that's why getModule
returns promise.
All module's results, saved in getModule.modules
object with relative path as a key and module result as a value.
Example:
Let's say, we have moduleA.js which requiring another modules and exporting variable someExport
and we want to use this module in browser.
Here is the code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dynamic Module Loading</title>
</head>
<body>
<script src="node_modules/als-require/require.js"></script>
<script>
getModule('./moduleA.js').then(someExport => {
window.someExport = someExport;
console.log(getModule.modules) // will return object with all modules and their results
});
</script>
</body>
</html>
In example above, first we include als-require
script which adding getModule
function and then, get the module.
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 Modules = require('als-require');
const modules = new Modules()
modules.require('./moduleA','moduleAVarname')
modules.require('./moduleB','moduleBVarname')
modules.scripts // the object with {relativePath:{exports,content}}
// Now you can save the script as file
require('fs').writeFileSync('test.js', modules.script);
// Or return it directly
app.get('/bundle.js', (req, res) => {
res.send(modules.script);
});
On Browser:
<script src="/bundle.js"></script>
<script>
console.log(moduleAVarname,moduleBVarname)
console.log(getModule.modules)
</script>
The bundle is self-sufficient and will include getModule with getModule.modules
which will include all bundled modules.
FAQs
A utility for using CommonJS require in the browser and creating bundles.
The npm package als-require receives a total of 21 weekly downloads. As such, als-require popularity was classified as not popular.
We found that als-require demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.