You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

4 years ago
  1. # import-cwd [![Build Status](https://travis-ci.org/sindresorhus/import-cwd.svg?branch=master)](https://travis-ci.org/sindresorhus/import-cwd)
  2. > Import a module like with [`require()`](https://nodejs.org/api/globals.html#globals_require) but from the current working directory
  3. ## Install
  4. ```
  5. $ npm install --save import-cwd
  6. ```
  7. ## Usage
  8. ```js
  9. const importCwd = require('import-cwd');
  10. // Target module is at '/Users/sindresorhus/unicorn/foo.js'
  11. console.log(__dirname);
  12. //=> '/Users/sindresorhus/rainbow'
  13. console.log(process.cwd());
  14. //=> '/Users/sindresorhus/unicorn'
  15. const foo = importCwd('./foo');
  16. ```
  17. ## API
  18. ### importCwd(moduleId)
  19. Like `require()`, throws when the module can't be found.
  20. ### importCwd.silent(moduleId)
  21. Returns `null` instead of throwing when the module can't be found.
  22. #### moduleId
  23. Type: `string`
  24. What you would use in `require()`.
  25. ## Related
  26. - [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path
  27. - [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module from a given path
  28. - [resolve-cwd](https://github.com/sindresorhus/resolve-cwd) - Resolve the path of a module from the current working directory
  29. - [resolve-pkg](https://github.com/sindresorhus/resolve-pkg) - Resolve the path of a package regardless of it having an entry point
  30. - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import modules lazily
  31. ## License
  32. MIT © [Sindre Sorhus](https://sindresorhus.com)