
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.
Import a module like with `require()` but from the current working directory
The import-cwd npm package allows users to import modules using a path relative to the current working directory (CWD). This can be particularly useful when you want to require modules without having to worry about the relative path from the current file to the module, especially in complex project structures.
Import module from current working directory
This feature allows you to import a module using a path relative to the current working directory. It's similar to require(), but it resolves paths from the CWD instead of the current file's directory.
const importCwd = require('import-cwd');
const myModule = importCwd('./my-module');
Silent import with fallback
This feature attempts to import a module, but if it fails (e.g., the module does not exist), it will return a fallback value without throwing an error. This is useful for providing default configurations or modules.
const importCwd = require('import-cwd');
const myModule = importCwd.silent('./non-existent-module', 'default-module');
This package allows you to require modules from a string of code. It's similar to import-cwd in that it provides an alternative way to require modules, but it does not specifically relate to the current working directory.
import-fresh allows you to import a module by bypassing the cache, ensuring you get a fresh copy of the module. While it provides a different feature from import-cwd, it also offers an alternative way to require modules.
resolve-from is a package that resolves the path of a module from a given directory. It's similar to import-cwd in that it helps with module resolution, but it focuses on resolving paths rather than importing modules directly.
Import a module like with
require()
but from the current working directory
$ npm install import-cwd
const importCwd = require('import-cwd');
// Target module is at '/Users/sindresorhus/unicorn/foo.js'
console.log(__dirname);
//=> '/Users/sindresorhus/rainbow'
console.log(process.cwd());
//=> '/Users/sindresorhus/unicorn'
const foo = importCwd('./foo');
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
Import a module like with `require()` but from the current working directory
The npm package import-cwd receives a total of 3,568,126 weekly downloads. As such, import-cwd popularity was classified as popular.
We found that import-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.