
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
resolve-cwd
Advanced tools
Resolve the path of a module like `require.resolve()` but from the current working directory
The resolve-cwd npm package is a utility that allows you to resolve the path of a module or file based on the current working directory (CWD). This can be particularly useful in scenarios where you need to require modules dynamically based on the execution context or when working with tools that change the current working directory.
Resolve module path from CWD
This feature allows you to resolve the path of a module from the current working directory. It's useful when you need to require a module dynamically and the module's location is relative to where the script is being executed.
const resolveCwd = require('resolve-cwd');
const modulePath = resolveCwd('some-module');
console.log(modulePath);
Silent failure on non-existent module
This feature provides a way to attempt to resolve a module's path without throwing an error if the module does not exist. It's useful for cases where you want to check for a module's presence and handle the absence gracefully.
const resolveCwd = require('resolve-cwd');
const modulePath = resolveCwd.silent('non-existent-module');
if (modulePath) {
console.log('Module found at:', modulePath);
} else {
console.log('Module not found');
}
The 'resolve' package is similar to resolve-cwd in that it helps in resolving the path of a module. However, 'resolve' is more general-purpose, offering more configuration options and the ability to resolve paths based on a given directory, not just the current working directory.
Similar to resolve-cwd, 'require-resolve' is designed to help with resolving the paths of modules. It differs in its approach by mimicking Node.js's require.resolve method but with additional features, such as the ability to resolve paths from specific directories.
Resolve the path of a module like
require.resolve()
but from the current working directory
$ npm install resolve-cwd
const resolveCwd = require('resolve-cwd');
console.log(__dirname);
//=> '/Users/sindresorhus/rainbow'
console.log(process.cwd());
//=> '/Users/sindresorhus/unicorn'
console.log(resolveCwd('./foo'));
//=> '/Users/sindresorhus/unicorn/foo.js'
Like require()
, throws when the module can't be found.
Returns undefined
instead of throwing when the module can't be found.
Type: string
What you would use in require()
.
MIT © Sindre Sorhus
FAQs
Resolve the path of a module like `require.resolve()` but from the current working directory
We found that resolve-cwd 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.