Socket
Socket
Sign inDemoInstall

import-cwd

Package Overview
Dependencies
2
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    import-cwd

Import a module like with `require()` but from the current working directory


Version published
Weekly downloads
3.3M
decreased by-17%
Maintainers
1
Install size
18.5 kB
Created
Weekly downloads
 

Package description

What is import-cwd?

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.

What are import-cwd's main functionalities?

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');

Other packages similar to import-cwd

Readme

Source

import-cwd Build Status

Import a module like with require() but from the current working directory

Install

$ npm install import-cwd

Usage

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');

API

importCwd(moduleId)

Like require(), throws when the module can't be found.

importCwd.silent(moduleId)

Returns undefined instead of throwing when the module can't be found.

moduleId

Type: string

What you would use in require().

  • import-from - Import a module from a given path
  • resolve-from - Resolve the path of a module from a given path
  • resolve-cwd - Resolve the path of a module from the current working directory
  • resolve-pkg - Resolve the path of a package regardless of it having an entry point
  • import-lazy - Import modules lazily
  • import-global - Import a globally installed module

License

MIT © Sindre Sorhus

Keywords

FAQs

Last updated on 27 Apr 2019

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc