Gathering detailed insights and metrics for import-from-esm
Gathering detailed insights and metrics for import-from-esm
Gathering detailed insights and metrics for import-from-esm
Gathering detailed insights and metrics for import-from-esm
Almost drop-in "import-from" replacement that supports loading both ESM & CJS modules
npm install import-from-esm
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2 Stars
286 Commits
2 Forks
2 Watching
21 Branches
3 Contributors
Updated on 04 Nov 2024
JavaScript (99.24%)
TypeScript (0.76%)
Cumulative downloads
Total Downloads
Last day
-1%
157,916
Compared to previous day
Last week
4.5%
910,189
Compared to previous week
Last month
3.8%
3,800,178
Compared to previous month
Last year
4,273.8%
30,744,706
Compared to previous year
2
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
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:
1(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:
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).require
(while the former is still in experimental).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.
$ npm install import-from-esm
1import importFrom from "import-from-esm"; 2 3// there is a file at `./foo/bar.{js,mjs,cjs,json}` 4 5await importFrom("foo", "./bar");
Like require()
, throws when the module can't be found.
Returns undefined
instead of throwing when the module can't be found.
Type: string
Directory to import from.
Type: string
What you would use in require()
.
Create a partial using a bound function if you want to import from the same fromDir
multiple times:
1const importFromFoo = importFrom.bind(null, "foo"); 2 3importFromFoo("./bar"); 4importFromFoo("./baz");
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
7 out of 7 merged PRs checked by a CI test -- score normalized to 10
Reason
20 different organizations found -- score normalized to 10
Details
Reason
no dangerous workflow patterns detected
Reason
update tool detected
Details
Reason
project is fuzzed
Details
Reason
license file detected
Details
Reason
15 commit(s) out of 30 and 0 issue activity out of 12 found in the last 90 days -- score normalized to 10
Reason
publishing workflow detected
Details
Reason
all dependencies are pinned
Details
Reason
SAST tool is run on all commits
Details
Reason
security policy file detected
Details
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
branch protection is not maximal on development and all release branches
Details
Reason
4 existing vulnerabilities detected
Details
Reason
badge detected: in_progress
Reason
found 29 unreviewed changesets out of 29 -- score normalized to 0
Score
Last Scanned on 2024-11-25T23:21:05Z
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More