{"id":16694261,"url":"https://github.com/sheerlox/import-from-esm","last_synced_at":"2025-03-17T00:33:32.478Z","repository":{"id":198598530,"uuid":"699936782","full_name":"sheerlox/import-from-esm","owner":"sheerlox","description":"Almost drop-in \"import-from\" replacement that supports loading both ESM \u0026 CJS modules","archived":false,"fork":false,"pushed_at":"2024-04-14T04:52:09.000Z","size":768,"stargazers_count":2,"open_issues_count":15,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-14T10:58:43.453Z","etag":null,"topics":["esm","import","module","path","resolve"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sheerlox.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-10-03T16:18:05.000Z","updated_at":"2024-04-15T16:10:31.437Z","dependencies_parsed_at":"2023-12-21T06:21:52.669Z","dependency_job_id":"8f315ecd-2fa7-4e55-9b13-344083d6a389","html_url":"https://github.com/sheerlox/import-from-esm","commit_stats":null,"previous_names":["sheerlox/import-from-esm"],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sheerlox%2Fimport-from-esm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sheerlox%2Fimport-from-esm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sheerlox%2Fimport-from-esm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sheerlox%2Fimport-from-esm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sheerlox","download_url":"https://codeload.github.com/sheerlox/import-from-esm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243835942,"owners_count":20355611,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["esm","import","module","path","resolve"],"created_at":"2024-10-12T16:44:51.151Z","updated_at":"2025-03-17T00:33:32.139Z","avatar_url":"https://github.com/sheerlox.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# import-from-esm\n\n[![Version](https://img.shields.io/npm/v/import-from-esm?logo=npm)](https://www.npmjs.com/package/import-from-esm)\n[![Monthly Downloads](https://img.shields.io/npm/dm/import-from-esm)](https://www.npmjs.com/package/import-from-esm)\n[![Test](https://img.shields.io/github/actions/workflow/status/sheerlox/import-from-esm/release.yml?logo=github)](https://github.com/sheerlox/import-from-esm/actions/workflows/release.yml)\n[![CodeQL](https://img.shields.io/github/actions/workflow/status/sheerlox/import-from-esm/codeql.yml?logo=github\u0026label=CodeQL)](https://github.com/sheerlox/import-from-esm/actions/workflows/codeql.yml)\n[![Coverage](https://img.shields.io/sonar/coverage/sheerlox_import-from-esm?logo=sonarcloud\u0026server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/summary/overall?id=sheerlox_import-from-esm)\n[![OpenSSF Scorecard](https://img.shields.io/ossf-scorecard/github.com/sheerlox/import-from-esm?label=openssf%20scorecard)\n](https://securityscorecards.dev/viewer/?uri=github.com/sheerlox/import-from-esm)\n\n## Overview\n\n\u003e Import a module like with [`require()`](https://nodejs.org/api/modules.html#modules_require_id) but from a given path (for ESM)\n\nThis library intends to be an _almost_ drop-in replacement of [`import-from`](https://github.com/sindresorhus/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`\n\n## Motivation\n\nThe main benefit of using `import-from` is that it abstracts the need to resolve the path and create a `require` statement. [Its code](https://github.com/sindresorhus/import-from/blob/v4.0.0/index.js) is really straightforward:\n\n\u003c!-- prettier-ignore-start --\u003e\n```js\n(fromDirectory, moduleId) =\u003e createRequire(path.resolve(fromDirectory, \"noop.js\"))(moduleId);\n```\n\u003c!-- prettier-ignore-end --\u003e\n\nIn the case of `import-from-esm`, there are a few additional benefits because of the way ESM works:\n\n1. 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](https://github.com/semantic-release/release-notes-generator/pull/544#issuecomment-1745455518)). You need to use `import.meta.resolve`, which is behind an experimental flag (although there's a ponyfill available at [wooorm/import-meta-resolve](https://github.com/wooorm/import-meta-resolve), which `import-from-esm` uses under-the-hood).\n2. 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](https://github.com/sheerlox/import-from-esm/blob/v1.3.1/index.js#L33-L37) import assertions or `require` (while the former is still in experimental).\n3. File extensions are now mandatory for relative paths. `import-from-esm` re-introduces [`require`'s file extension discovery](https://nodejs.org/docs/latest-v18.x/api/modules.html#file-modules).\n\nAs you can see, there is quite a bit of complexity that [is abstracted behind `import-from-esm`](https://github.com/sheerlox/import-from-esm/blob/v1.3.1/index.js). The first bullet point issue affected both [`@semantic-release/commit-analyzer`](https://github.com/semantic-release/commit-analyzer/pull/537/files#diff-a558e4411f9515691b462dfd89640ec649509db79a4a86c5c8860d7bff173f95R28) and [`@semantic-release/release-notes-generator`](https://github.com/semantic-release/release-notes-generator/pull/544/files#diff-bee027b39eb704f3c940d54960f4f26693260c52d72707ac17d72f38f66da7d5R30). 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.\n\nAs 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.\n\n## Install\n\n```\n$ npm install import-from-esm\n```\n\n## Usage\n\n```js\nimport importFrom from \"import-from-esm\";\n\n// there is a file at `./foo/bar.{js,mjs,cjs,json}`\n\nawait importFrom(\"foo\", \"./bar\");\n```\n\n## API\n\n### importFrom(fromDirectory, moduleId)\n\nLike `require()`, throws when the module can't be found.\n\n### importFrom.silent(fromDirectory, moduleId)\n\nReturns `undefined` instead of throwing when the module can't be found.\n\n#### fromDirectory\n\nType: `string`\n\nDirectory to import from.\n\n#### moduleId\n\nType: `string`\n\nWhat you would use in `require()`.\n\n## Tip\n\nCreate a partial using a bound function if you want to import from the same `fromDir` multiple times:\n\n```js\nconst importFromFoo = importFrom.bind(null, \"foo\");\n\nimportFromFoo(\"./bar\");\nimportFromFoo(\"./baz\");\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsheerlox%2Fimport-from-esm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsheerlox%2Fimport-from-esm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsheerlox%2Fimport-from-esm/lists"}