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.

70 lines
1.6 KiB

4 years ago
  1. # import-from [![Build Status](https://travis-ci.org/sindresorhus/import-from.svg?branch=master)](https://travis-ci.org/sindresorhus/import-from)
  2. > Import a module like with [`require()`](https://nodejs.org/api/globals.html#globals_require) but from a given path
  3. ## Install
  4. ```
  5. $ npm install --save import-from
  6. ```
  7. ## Usage
  8. ```js
  9. const importFrom = require('import-from');
  10. // There is a file at `./foo/bar.js`
  11. importFrom('foo', './bar');
  12. ```
  13. ## API
  14. ### importFrom(fromDir, moduleId)
  15. Like `require()`, throws when the module can't be found.
  16. ### importFrom.silent(fromDir, moduleId)
  17. Returns `null` instead of throwing when the module can't be found.
  18. #### fromDir
  19. Type: `string`
  20. Directory to import from.
  21. #### moduleId
  22. Type: `string`
  23. What you would use in `require()`.
  24. ## Tip
  25. Create a partial using a bound function if you want to import from the same `fromDir` multiple times:
  26. ```js
  27. const importFromFoo = importFrom.bind(null, 'foo');
  28. importFromFoo('./bar');
  29. importFromFoo('./baz');
  30. ```
  31. ## Related
  32. - [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory
  33. - [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module from a given path
  34. - [resolve-cwd](https://github.com/sindresorhus/resolve-cwd) - Resolve the path of a module from the current working directory
  35. - [resolve-pkg](https://github.com/sindresorhus/resolve-pkg) - Resolve the path of a package regardless of it having an entry point
  36. - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import modules lazily
  37. ## License
  38. MIT © [Sindre Sorhus](https://sindresorhus.com)