Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

import-from-esm

sheerlox4.8mMIT2.0.0TypeScript support: included

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

import, module, resolve, path, esm

readme

import-from-esm

Version Monthly Downloads Test CodeQL Coverage OpenSSF Scorecard

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

Motivation

The main benefit of using import-from is that it abstracts the need to resolve the path and create a require statement. Its code is really straightforward:

(fromDirectory, moduleId) => createRequire(path.resolve(fromDirectory, "noop.js"))(moduleId);

In the case of import-from-esm, there are a few additional benefits because of the way ESM works:

  1. Importing a package installed along a library (in the parent application) from that library is no longer possible (which was the issue that made me work on this library). You need to use import.meta.resolve, which is behind an experimental flag (although there's a ponyfill available at wooorm/import-meta-resolve, which import-from-esm uses under-the-hood).
  2. If the file you're trying to import (whether relative, package, export map, etc ...) is a JSON file, you need to detect that and use import assertions or require (while the former is still in experimental).
  3. File extensions are now mandatory for relative paths. import-from-esm re-introduces require's file extension discovery.

As you can see, there is quite a bit of complexity that is abstracted behind import-from-esm. The first bullet point issue affected both @semantic-release/commit-analyzer and @semantic-release/release-notes-generator. After spending hours on research to solve the issue, I realized that the work I was doing would benefit others as well, so I decided to create a package out of it.

As a proponent of ESM, I have put a lot of thought into poly-filling require features for import, but finally came to the conclusion that developing a package to facilitate the ecosystem transition to ESM by reducing friction was a good thing.

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");

changelog

2.0.0 (2024-12-31)

⚠ Breaking changes

  • drop support for Node 16

🚨 Tests

  • remove --no-warnings Node option (bd75fce)

⚙️ Continuous Integrations

  • action: add Node 22 to test matrix (9bced1f)
  • action: allow all sigstore subdomains (c63bbc0)
  • action: allow new scorecard endpoint (c45dd26)
  • action: don't run fuzzing tests on Windows + Node 22 (0ff7550)
  • action: enable npm provenance (0f85491)
  • action: run each test separately (32c1d4b)
  • action: update actions/checkout action to v4.2.2 (#114) (fe4cb47)
  • action: update actions/download-artifact action to v4.1.8 (#117) (5e32ca2)
  • action: update actions/download-artifact digest to c850b93 (#96) (944cc05)
  • action: update actions/setup-node action to v4.1.0 (#116) (a28015f)
  • action: update actions/setup-node digest to 60edb5d (#98) (9b2c96b)
  • action: update actions/upload-artifact action to v4.3.1 (#90) (1990871)
  • action: update actions/upload-artifact action to v4.5.0 (#112) (829be50)
  • action: update actions/upload-artifact digest to 5d5d22a (#89) (0454435)
  • action: update github/codeql-action action to v3.25.0 (#93) (dcbb763)
  • action: update github/codeql-action action to v3.28.0 (#109) (3fa778e)
  • action: update insurgent-lab/is-in-pr-action action to v0.2.0 (#97) (6f3650b)
  • action: update ossf/scorecard-action action to v2.4.0 (#118) (c33184e)
  • action: update sonarsource/sonarcloud-github-action action to v2.3.0 (#120) (2d9e917)
  • action: update step-security/harden-runner action to v2.10.2 (#115) (3bcace5)
  • action: update step-security/harden-runner action to v2.7.0 (#95) (cb020c9)
  • renovate: don't update Node.js in .tool-versions (27a1832)
  • renovate: use correct preset & fix node package name (2e3cb36)

♻️ Chores

  • deps: lock file maintenance (144854b)
  • deps: lock file maintenance (4c5aeb8)
  • deps: lock file maintenance (ac1f305)
  • deps: lock file maintenance (9d38657)
  • deps: lock file maintenance (6579691)
  • deps: lock file maintenance (50f2f1f)
  • deps: lock file maintenance (d2a439a)
  • deps: lock file maintenance (04af189)
  • deps: lock file maintenance (1419cda)
  • deps: lock file maintenance (3a55659)
  • deps: lock file maintenance (e43cca2)
  • deps: lock file maintenance (69caf26)
  • deps: lock file maintenance (4fd9496)
  • deps: lock file maintenance (660b63b)
  • deps: lock file maintenance (169e8e9)
  • deps: lock file maintenance (72f3836)
  • deps: lock file maintenance (91398fd)
  • deps: lock file maintenance (#105) (076c959)
  • deps: lock file maintenance (#119) (445c8c9)
  • deps: lock file maintenance (#125) (f1c57df)
  • deps: lock file maintenance (#132) (6759be6)
  • deps: lock file maintenance (#135) (b786962)
  • deps: migrate to @insurgent/conventional-changelog-preset package (2d5922f)
  • deps: remove unused cross-env dependency (2aab0db)
  • deps: update dependency semantic-release to v23.0.8 (#106) (4a8a3b7)
  • deps: update dependency semantic-release to v23.1.1 (5a571d3)
  • deps: update dependency semantic-release to v24.1.0 (48917e8)
  • deps: update dependency semantic-release to v24.1.1 (#131) (8cb82bf)
  • deps: update dependency semantic-release to v24.1.2 (7cbed1e)
  • deps: update dependency semantic-release to v24.1.3 (#133) (92b7d2d)
  • deps: update dependency semantic-release to v24.2.0 (f1e4e68)
  • deps: update dependency tsd to v0.31.1 (b086815)
  • deps: update dependency tsd to v0.31.2 (#130) (99b1e13)
  • deps: update semantic-release related packages (#121) (95ad0d5)
  • drop support for Node 16 (c8ec660)
  • update knip ignores (afc6749)
  • update semantic-release config (ba64afa)

1.3.4 (2024-04-15)

🐛 Bug Fixes

  • add support for importing index files from subpaths (#108) (ad58e26)

🚨 Tests

  • re-enable 100% coverage threshold (ec9280d)

⚙️ Continuous Integrations

  • action: update actions/download-artifact action to v4 (#70) (74c3833)
  • action: update actions/download-artifact digest to 6b208ae (#76) (75e09ef)
  • action: update actions/setup-node digest to b39b52d (#77) (1c73611)
  • action: update actions/upload-artifact action to v4 (#71) (bec5974)
  • action: update actions/upload-artifact action to v4.2.0 (#85) (7aa90b5)
  • action: update actions/upload-artifact digest to 694cdab (#84) (7140c09)
  • action: update github/codeql-action action to v2.22.11 (#65) (e484313)
  • action: update github/codeql-action action to v2.22.8 (#59) (6da3637)
  • action: update github/codeql-action action to v3 (#72) (6d6c2ff)
  • action: update github/codeql-action action to v3.23.1 (#79) (33c09ff)
  • action: update insurgent-lab/is-in-pr-action action to v0.1.5 (#60) (66c27c6)
  • action: update sonarsource/sonarcloud-github-action action to v2.1.1 (#68) (0b9df9f)
  • add alpha branch to workfows (2b6df31)
  • allow egress to nodejs.org (f6ffcc4)
  • setup release on branch "alpha" (a05592d)

♻️ Chores

  • deps: lock file maintenance (311405b)
  • deps: lock file maintenance (5446885)
  • deps: lock file maintenance (7aaecc2)
  • deps: lock file maintenance (04d0591)
  • deps: lock file maintenance (b41156b)
  • deps: lock file maintenance (9055fb5)
  • deps: lock file maintenance (1ecdca8)
  • deps: lock file maintenance (b6f76f8)
  • deps: lock file maintenance (3535b80)
  • deps: lock file maintenance (#104) (8bb2b78)
  • deps: lock file maintenance (#62) (ab31bce)
  • deps: lock file maintenance (#69) (bfcc047)
  • deps: lock file maintenance (#75) (6ee8872)
  • deps: lock file maintenance (#94) (bdea181)
  • deps: update dependency @fast-check/ava to v1.2.0 (6119885)
  • deps: update dependency @fast-check/ava to v1.2.1 (#78) (01800ce)
  • deps: update dependency @insurgent/export-map-test to v1.0.1 (20c1240)
  • deps: update dependency c8 to v9 (#82) (24533c1)
  • deps: update dependency knip to v2.41.3 (#57) (595c796)
  • deps: update dependency knip to v2.41.5 (#61) (a49d8a0)
  • deps: update dependency knip to v2.41.6 (47265d7)
  • deps: update dependency knip to v2.42.0 (474df64)
  • deps: update dependency knip to v2.43.0 (2651797)
  • deps: update dependency knip to v3 (#63) (e233c72)
  • deps: update dependency knip to v3.10.0 (0092258)
  • deps: update dependency knip to v3.12.0 (#83) (6a7b5fe)
  • deps: update dependency knip to v3.13.0 (4629d01)
  • deps: update dependency knip to v3.13.1 (5a21529)
  • deps: update dependency knip to v3.13.2 (b666148)
  • deps: update dependency knip to v3.3.0 (5591445)
  • deps: update dependency knip to v3.3.2 (b2590a3)
  • deps: update dependency knip to v3.3.5 (#64) (82310f7)
  • deps: update dependency knip to v3.4.0 (751e794)
  • deps: update dependency knip to v3.5.0 (d224fcc)
  • deps: update dependency knip to v3.5.1 (eb85c5e)
  • deps: update dependency knip to v3.6.0 (3cd5d1d)
  • deps: update dependency knip to v3.6.1 (d474580)
  • deps: update dependency knip to v3.7.0 (262db0e)
  • deps: update dependency knip to v3.7.1 (92b81c0)
  • deps: update dependency knip to v3.8.1 (#74) (5901212)
  • deps: update dependency knip to v3.8.2 (ce76cd9)
  • deps: update dependency knip to v3.8.3 (44863ca)
  • deps: update dependency knip to v3.8.4 (d30c5ae)
  • deps: update dependency knip to v3.9.0 (ac3dbaa)
  • deps: update dependency knip to v4 (#87) (fd3c72d)
  • deps: update dependency knip to v4.0.3 (f18d6c1)
  • deps: update dependency knip to v4.0.4 (#88) (24661dd)
  • deps: update dependency knip to v4.1.0 (3f7d225)
  • deps: update dependency knip to v4.2.0 (1debd26)
  • deps: update dependency knip to v4.3.0 (#91) (30c3aa9)
  • deps: update dependency knip to v4.3.1 (833991b)
  • deps: update dependency knip to v4.4.0 (e2130e3)
  • deps: update dependency knip to v4.5.0 (#101) (c9ae1ba)
  • deps: update dependency knip to v4.6.0 (2918bdb)
  • deps: update dependency semantic-release to v22.0.10 (#67) (26b45f6)
  • deps: update dependency semantic-release to v22.0.12 (#73) (84320df)
  • deps: update dependency semantic-release to v22.0.8 (#58) (f32eb14)
  • deps: update dependency semantic-release to v23 (#86) (df3e70d)
  • deps: update dependency semantic-release to v23.0.1 (#99) (792b168)
  • deps: update dependency semantic-release to v23.0.2 (6b2011e)
  • deps: update dependency tsd to v0.30.0 (cce491e)
  • deps: update dependency tsd to v0.30.1 (e750e1b)
  • deps: update dependency tsd to v0.30.2 (8c9b69c)
  • deps: update dependency tsd to v0.30.3 (a01e420)
  • deps: update dependency tsd to v0.30.4 (e73701e)
  • deps: update dependency tsd to v0.30.5 (8203291)
  • deps: update dependency tsd to v0.30.6 (6e633e9)
  • deps: update dependency tsd to v0.30.7 (d71588e)
  • deps: update dependency tsd to v0.31.0 (0c3e3c8)
  • readme: add monthly downloads badge (b6da755)
  • readme: remove "PRs welcome" badge (5f70320)

1.3.3 (2023-11-19)

🐛 Bug Fixes

  • set Promise return types in declaration file (6ca7d3c)

🚨 Tests

1.3.2 (2023-11-19)

🛠 Builds

  • npm: update keywords and description (35e2aaa)

📚 Documentation

  • readme: add motivation section (302732f)

⚙️ Continuous Integrations

  • action: update github/codeql-action action to v2.22.7 (#47) (22f4d00)
  • action: update insurgent-lab/is-in-pr-action action to v0.1.4 (#56) (f7e4812)
  • action: update step-security/harden-runner action to v2.6.1 (#54) (40b0da8)

♻️ Chores

1.3.1 (2023-11-16)

📚 Documentation

  • readme: update overview and usage sections (2d4ae40)

1.3.0 (2023-11-16)

✨ Features

  • support index, main and extensionless JSON modules (#51) (f5486fd)

📦 Code Refactoring

1.2.2 (2023-11-16)

🐛 Bug Fixes

  • typescript: use default export in declaration file (#48) (2d0a397)

♻️ Chores

  • deps: lock file maintenance (b021df5)
  • deps: lock file maintenance (36d42e8)
  • deps: update dependency knip to v2.40.2 (#45) (1a5e32d)
  • deps: update dependency knip to v2.41.0 (d3d8eab)

1.2.1 (2023-11-09)

📚 Documentation

  • readme: remove known limitation disclaimer (c5d7bdd)

1.2.0 (2023-11-08)

✨ Features

🚨 Tests

  • add fuzzing regression tests (#43) (2866dbd)
  • increase fuzz tests verbosity (fea8573)

⚙️ Continuous Integrations

  • action: pin insurgent-lab/is-in-pr-action action to cae57fd (#41) (29fbee7)
  • action: prevent duplicate checks on Renovate PRs (ce97a59)

♻️ Chores

  • deps: update dependency semantic-release to v22.0.7 (#38) (e46ca32)
  • update "if file module" comment (2fd56a9)

1.1.3 (2023-11-07)

🐛 Bug Fixes

♻️ Chores

  • deps: update dependency knip to v2.39.0 (#39) (44f92c3)

1.1.2 (2023-11-06)

📚 Documentation

  • readme: add known limitation disclaimer (b8caf20)

♻️ Chores

💎 Styles

1.1.1 (2023-11-06)

🐛 Bug Fixes

  • remove postinstall script (818c4d5)

1.1.0 (2023-11-06)

✨ Features

🐛 Bug Fixes

  • support extensionless subpath package access (5bb1b09)

🚨 Tests

  • add package import test cases (#34) (8d1fa2c)
  • resolve from nested package path (836a148)

♻️ Chores

  • deps: lock file maintenance (#33) (5bb144a)
  • deps: pin dependency cross-env to 7.0.3 (#32) (33f281a)

1.0.3 (2023-11-05)

🐛 Bug Fixes

  • loading absolute path fails on windows (4a0fbd0)

⚙️ Continuous Integrations

  • action: also run tests on macos & windows (df039b1)
  • action: increase test workflow timeout (f08a25f)
  • action: update actions/setup-node action to v4 (#29) (84c8f34)
  • action: update github/codeql-action action to v2.22.5 (#30) (05ed346)
  • action: update ossf/scorecard-action action to v2.3.1 (#28) (a03e669)
  • action: update workflows (d1e7069)
  • action: update workflows (111c720)
  • action: whitelist npm signatures audit domain (3fbe0e5)

♻️ Chores

  • add cross-env (f55d6db)
  • deps: lock file maintenance (509865c)
  • deps: lock file maintenance (#26) (97d9103)
  • deps: update dependency knip to v2.37.0 (36967bc)
  • deps: update dependency knip to v2.38.0 (cdb528a)
  • deps: update dependency knip to v2.38.1 (4a240f2)
  • deps: update dependency knip to v2.38.4 (6b4e63f)
  • deps: update dependency knip to v2.38.5 (54f917b)
  • deps: update dependency knip to v2.38.6 (4a1604c)
  • deps: update dependency semantic-release to v22.0.6 (b78f3aa)
  • fix knip config (2f74381)

1.0.2 (2023-10-21)

🛠 Builds

📚 Documentation

🚨 Tests

⚙️ Continuous Integrations

  • action: enable ossf scorecard repo_token (390edd7)
  • action: update actions/checkout action to v4.1.1 (#23) (fcdb52c)
  • action: update actions/checkout digest to b4ffde6 (#24) (93c99ed)
  • action: update github/codeql-action action to v2.22.2 (#17) (7d49e7f), closes #9
  • action: update github/codeql-action action to v2.22.3 (#20) (3611086), closes #9
  • action: update github/codeql-action action to v2.22.3 (#21) (45faedc)
  • action: update github/codeql-action action to v2.22.4 (#25) (fe13382)

♻️ Chores

1.0.1 (2023-10-12)

📚 Documentation

  • readme: update badges (4551845)
  • remove TODO.md & update RESOURCES.md (3768077)

⚙️ Continuous Integrations

  • action: add sonarcloud scan to test workflow (ab3ff0e)
  • action: apply step-security recommended policies (c674db6)
  • action: replace codecov with sonarcloud (4037b3b)

♻️ Chores

  • deps: pin dependencies (#16) (cb41d6b)
  • move test files to a dedicated folder (acbecd3)
  • move tsd files to tests folder (96d3297)
  • sonarcloud: exclude test files from coverage (ab4cdad)
  • update LICENSE (c6f8d74)

1.0.0 (2023-10-11)

⚠ Breaking changes

  • now only returns the default export to match import-from behavior

⚙️ Continuous Integrations

  • action: apply step-security recommended policies (5d8effa)
  • action: fix test workflow running twice (6e7819a)
  • action: set release workflow permissions as read-only (73403ee)

🗑 Reverts

  • revert "feat!: export whole module instead of default export" (7879e2f)

0.1.1 (2023-10-11)

🐛 Bug Fixes

  • fix exports subpath resolution (9c28bd1)

📚 Documentation

  • readme: add badges & silent() usage note (568b320)

🚨 Tests

  • add code coverage checks (9c283ce)

⚙️ Continuous Integrations

  • action: add combined job for branch protection (25fd48b)
  • action: add ossf scorecard workflow (00b9f08)
  • action: apply step-security best practices (#11) (f5b3002)
  • action: run linting checks in test workflow (058505a)
  • action: setup semantic-release (68f69da)
  • action: update actions/checkout action to v4 (#13) (c3ff261)
  • action: update actions/checkout action to v4 (#8) (8b31bc2)
  • action: update actions/upload-artifact action to v3.1.3 (#4) (6f797d1)
  • action: update github/codeql-action action to v2.22.0 (#6) (dbe69dc)
  • action: update github/codeql-action action to v2.22.1 (#14) (0820b88), closes #9
  • action: update ossf/scorecard-action action to v2.2.0 (#7) (3493169)
  • action: update ossf/scorecard-action action to v2.3.0 (#12) (163fc5c)
  • action: use npm ci instead of npm install (28395b0)
  • renovate: configure renovate (d57a8d5)

♻️ Chores

💎 Styles

🗑 Reverts

  • revert "ci(action): update github/codeql-action action to v2.22.0 (#6)" (#9) (9cf8422)

0.1.0 (2023-10-06)

⚠ Breaking changes

  • export whole module instead of default export
  • importFrom and importFrom.silent now return promises
  • file extentions now must be specified
  • import-from-esm is now a native ES Module.

✨ Features

  • export whole module instead of default export (ec44587)

🛠 Builds

  • npm: update minimum node version (c0ae5ad)

📦 Code Refactoring

  • convert to pure esm (f873e47)
  • replicate CJS module resolution outlines (4128481)
  • support importing ESM modules (naive approach) (69d0166)

🚨 Tests

  • improve tests by testing for various extensions (938c6bd)
  • test both CJS & ESM modules (16d85b4)

⚙️ Continuous Integrations

  • action: update node & actions versions (b1c50cc)

♻️ Chores

  • add TODO.md (ba7abbe)
  • various changes after repository transfer (786824a)