
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
babel-plugin-module-deps
Advanced tools
This Babel plugin redefines require
within all (CJS) modules to record any
required modules in an array of resolved filenames called module.deps
.
(Note that the array may have duplicates.)
It only works with
Node.js CommonJS modules
(though with
@babel/plugin-transform-modules-commonjs
you can still use ESM syntax).
The module.deps
arrays let a tool later discover what other modules
each module depends on, by inspecting require.cache[filename].deps
.
This can be useful to deep-reload a module, or to detect when a module
should be called according to whether a generated file is older
than the module or dependency code.
Add the module as a plugin to your Babel configuration, as in:
{
"plugins": ["babel-plugin-module-deps"]
}
Generally you want this plugin to run last,
so list it last
in the "plugins"
array.
In particular, this plugin should run after
@babel/plugin-transform-modules-commonjs
(if you're using ESM syntax in CJS),
so that require
is redefined before import
s get executed
(as they are simulated by require
).
Then you can access the current module's dependencies via module.deps
,
or an arbitrary module's dependencies via require.cache[filename].deps
,
where filename
is the require.resolve
d filename for a module.
If you've just run require(modname)
with
@babel/register
configured to run this plugin, then calling the function below as
walkDeps(modname)
should give an array of all recursive module dependencies
that modname
require
s or import
s as require.resolve
d filenames
(including require.resolve(modname)
itself, but excluding duplicates).
function walkDeps(modname) {
const deps = {};
function recurse(submodname) {
deps[submodname] = true;
const submod = require.cache[submodname];
if (!submod) return;
const subdeps = submod.deps;
if (!subdeps) return;
for (dep of subdeps)
if (!dep in deps)
recurse(dep);
}
recurse(require.resolve(modname));
return Object.keys(deps);
}
FAQs
Babel plugin to annotate CJS modules with what they require
The npm package babel-plugin-module-deps receives a total of 2 weekly downloads. As such, babel-plugin-module-deps popularity was classified as not popular.
We found that babel-plugin-module-deps demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.