pax_global_header00006660000000000000000000000064134610273320014513gustar00rootroot0000000000000052 comment=fb039dcf3fe69bb3d395be4b5db2ab9367efcca7 import-from-3.0.0/000077500000000000000000000000001346102733200137665ustar00rootroot00000000000000import-from-3.0.0/.editorconfig000066400000000000000000000002571346102733200164470ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 import-from-3.0.0/.gitattributes000066400000000000000000000000231346102733200166540ustar00rootroot00000000000000* text=auto eol=lf import-from-3.0.0/.gitignore000066400000000000000000000000271346102733200157550ustar00rootroot00000000000000node_modules yarn.lock import-from-3.0.0/.npmrc000066400000000000000000000000231346102733200151010ustar00rootroot00000000000000package-lock=false import-from-3.0.0/.travis.yml000066400000000000000000000000651346102733200161000ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' import-from-3.0.0/fixture/000077500000000000000000000000001346102733200154545ustar00rootroot00000000000000import-from-3.0.0/fixture/fixture.js000066400000000000000000000000521346102733200174750ustar00rootroot00000000000000'use strict'; module.exports = 'unicorn'; import-from-3.0.0/index.d.ts000066400000000000000000000022371346102733200156730ustar00rootroot00000000000000declare const importFrom: { /** Import a module like with [`require()`](https://nodejs.org/api/modules.html#modules_require_id) but from a given path. @param fromDirectory - Directory to import from. @param moduleId - What you would use in `require()`. @throws Like `require()`, throws when the module can't be found. @example ``` import importFrom = require('import-from'); try { const bar = importFrom('foo', './bar'); // Do something with `bar` } catch (error) { // `error` is thrown when `./bar` can't be found } ``` */ (fromDirectory: string, moduleId: string): unknown; /** Import a module like with [`require()`](https://nodejs.org/api/modules.html#modules_require_id) but from a given path. @param fromDirectory - Directory to import from. @param moduleId - What you would use in `require()`. @returns `undefined` instead of throwing when the module can't be found. @example ``` import importFrom = require('import-from'); const bar = importFrom.silent('foo', './bar'); // Do something with `bar`, may be `undefined` when `./bar` can't be found ``` */ silent(fromDirectory: string, moduleId: string): unknown; }; export = importFrom; import-from-3.0.0/index.js000066400000000000000000000004431346102733200154340ustar00rootroot00000000000000'use strict'; const resolveFrom = require('resolve-from'); module.exports = (fromDirectory, moduleId) => require(resolveFrom(fromDirectory, moduleId)); module.exports.silent = (fromDirectory, moduleId) => { try { return require(resolveFrom(fromDirectory, moduleId)); } catch (_) {} }; import-from-3.0.0/index.test-d.ts000066400000000000000000000001421346102733200166410ustar00rootroot00000000000000import importFrom = require('.'); importFrom('foo', './bar'); importFrom.silent('foo', './bar'); import-from-3.0.0/license000066400000000000000000000021251346102733200153330ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import-from-3.0.0/package.json000066400000000000000000000012301346102733200162500ustar00rootroot00000000000000{ "name": "import-from", "version": "3.0.0", "description": "Import a module like with `require()` but from a given path", "license": "MIT", "repository": "sindresorhus/import-from", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "require", "resolve", "path", "module", "from", "like", "import", "path" ], "dependencies": { "resolve-from": "^5.0.0" }, "devDependencies": { "ava": "^1.4.1", "tsd": "^0.7.2", "xo": "^0.24.0" } } import-from-3.0.0/readme.md000066400000000000000000000033611346102733200155500ustar00rootroot00000000000000# import-from [![Build Status](https://travis-ci.org/sindresorhus/import-from.svg?branch=master)](https://travis-ci.org/sindresorhus/import-from) > Import a module like with [`require()`](https://nodejs.org/api/modules.html#modules_require_id) but from a given path ## Install ``` $ npm install import-from ``` ## Usage ```js const importFrom = require('import-from'); // There is a file at `./foo/bar.js` importFrom('foo', './bar'); ``` ## API ### importFrom(fromDirectory, moduleId) Like `require()`, throws when the module can't be found. ### importFrom.silent(fromDirectory, moduleId) Returns `undefined` instead of throwing when the module can't be found. #### fromDirectory Type: `string` Directory to import from. #### moduleId Type: `string` What you would use in `require()`. ## Tip Create a partial using a bound function if you want to import from the same `fromDir` multiple times: ```js const importFromFoo = importFrom.bind(null, 'foo'); importFromFoo('./bar'); importFromFoo('./baz'); ``` ## Related - [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory - [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module from a given path - [resolve-cwd](https://github.com/sindresorhus/resolve-cwd) - Resolve the path of a module from the current working directory - [resolve-pkg](https://github.com/sindresorhus/resolve-pkg) - Resolve the path of a package regardless of it having an entry point - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import modules lazily - [import-global](https://github.com/sindresorhus/import-global) - Import a globally installed module ## License MIT © [Sindre Sorhus](https://sindresorhus.com) import-from-3.0.0/test.js000066400000000000000000000011231346102733200153000ustar00rootroot00000000000000import test from 'ava'; import importFrom from '.'; test('importFrom()', t => { t.is(importFrom('fixture', './fixture'), 'unicorn'); t.throws(() => importFrom('fixture', './nonexistent')); const moduleNotFoundError = t.throws(() => { importFrom('fixture', './nonexistent'); }, Error); t.is(moduleNotFoundError.code, 'MODULE_NOT_FOUND'); t.regex(moduleNotFoundError.message, /^Cannot find module '.\/nonexistent'/); }); test('importFrom.silent()', t => { t.is(importFrom.silent('fixture', './fixture'), 'unicorn'); t.is(importFrom.silent('fixture', './nonexistent'), undefined); });