1. import-from
Import a module like with `require()` but from a given path
import-from
Package: import-from
Created by: sindresorhus
Last modified: Sat, 17 Jun 2023 00:13:35 GMT
Version: 4.0.0
License: MIT
Downloads: 31,976,789
Repository: https://github.com/sindresorhus/import-from

Install

npm install import-from
yarn add import-from

import-from

Import a module like with require() but from a given path

Install

$ npm install import-from

Usage

 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:

 const importFromFoo = importFrom.bind(null, 'foo');

importFromFoo('./bar');
importFromFoo('./baz');
  • import-cwd - Import a module from the current working directory
  • 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

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code