Package Exports
- import-from-esm
- import-from-esm/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (import-from-esm) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
import-from-esm
Overview
Import a module like with
require()
but from a given path (for ESM)
This library intends to be an almost drop-in replacement of import-from
(from which it is forked), exposing the same API and behavior but also supporting ES modules (ESM). Just add await
before importFrom
/importFrom.silent
Install
$ npm install import-from-esm
Usage
import importFrom from "import-from-esm";
// there is a file at `./foo/bar.{js,mjs,cjs,json}`
await 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");