
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
cjs-module
Advanced tools
Environment agnostic CJS (Node.js) modules resolver.
It implements a strict version of Node.js modules resolution logic, differences are as follows:
/
) are supported in require's path arguments (Background: even though Node.js internally seems to follow Windows path separator in Windows environment, it won't work in *nix environments, and even in Window env it's not reliable so by all means should be avoided)resolve(dir, 'fs')
will naturally result with null$ npm install cjs-module
For provided configuration, returns a CJS modules resolver:
['.js', '.json', '.node']
confirmFile(filepath)
function. Confirms whether there's a module at provided (not normalized, absolute) file path. Returns promise-like object which resolves with either normalized full path of a module or null (if there's no module for given path).resolvePackageMain(dirpath)
function. Returns value of package.json's main
property for given path. Returns promise-like object which resolves with either resolved value, or null, when either package.json
file was not found, or it didn't have main property.confirmFile
resolution can be synchronous.Node.js resolver
Asynchronously resolves module path against provided directory path.
Returns promise. If no matching module was found, promise resolves with null
otherwise
full module path becomes a resolved value.
var resolve = require("cjs-module/resolve");
// Asynchronously resolve path for 'foo' module against current path
resolve(__dirname, "foo").done(function(fooModulePath) {
if (!fooModulePath) {
// 'foo' module doesn't exist
} else {
// 'foo' module found at fooModulePath
}
});
Node.js resolver
Synchronously resolves module path against provided directory path.
If matching module was found then full module path is returned, otherwise null
.
var resolveSync = require("cjs-module/resolve/sync");
// Synchronously resolve path for 'foo' module against current path
var fooModulePath = resolveSync(__dirname, "foo");
if (!fooModulePath) {
// 'foo' module doesn't exist
} else {
// 'foo' module found
}
Whether provided path is a root of a package
var isPackageRoot = require("cjs-module/is-package-root");
isPackageRoot(dirPath).done(function(isRoot) {
if (isRoot) {
// Provided path is package root
}
});
Resolve package root path for provided path. It is about resolution of first upper package root
var resolvePackageRoot = require("cjs-module/resolve-package-root");
resolvePackageRoot(dirPath).done(function(root) {
if (!root) {
// Provided path is not located in any package
}
});
Resolve project root path for provided path. It is about resolution of topmost package root for given path
var resolveProjectRoot = require("cjs-module/resolve-project-root");
resolveProjectRoot(dirPath).done(function(root) {
if (!root) {
// Provided path is not located in any project
}
});
Resolve all module dependencies. Returns promise that resolves with an array of paths, that includes path to input module and paths to all its dependencies (it includes deep dependencies, so also dependencies of the dependencies)
var getDependencies = require("cjs-module/get-dependencies");
getDependencies(modulePath).done(function(deps) {
console.log(deps); // e.g. [pathToModulePath, pathToDep1, pathToDep2, ...pathToDepn]
});
$ npm test
FAQs
CJS (Node.js) style modules resolver
The npm package cjs-module receives a total of 194 weekly downloads. As such, cjs-module popularity was classified as not popular.
We found that cjs-module 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
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.