{"name":"import-from-esm","version":"2.0.0","main":"index.js","type":"module","description":"Import a module like with require() but from a given path (for ESM)","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sheerlox/import-from-esm.git"},"author":{"name":"Pierre Cavin","email":"me@sherlox.io","url":"https://github.com/sheerlox"},"contributors":[{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"}],"engines":{"node":">=18.20"},"scripts":{"test":"npm run test:all && npm run knip","test:all":"npm run test:ava && npm run test:nested && npm run test:fuzz:regression && npm run test:fuzz && npm run test:tsd","test:ava":"c8 ava tests/e2e.test.js","test:ava:watch":"c8 ava tests/e2e.test.js --watch","test:nested":"cd tests/fixture/nested/ && npm ci && npm test","test:fuzz":"ava tests/fuzz.test.js","test:fuzz:regression":"ava tests/fuzz-regression.test.js","test:tsd":"tsd --files tests/**/*.test-d.ts","knip":"knip","lint":"xo","lint:fix":"xo --fix"},"keywords":["import","module","resolve","path","esm"],"dependencies":{"debug":"^4.3.4","import-meta-resolve":"^4.0.0"},"devDependencies":{"@fast-check/ava":"1.2.1","@insurgent/export-map-test":"1.0.1","@insurgent/json-index-test":"2.0.0","@insurgent/json-main-test":"2.0.0","@insurgent/subpaths-test":"1.0.0","@insurgent/conventional-changelog-preset":"10.0.0","@semantic-release/changelog":"6.0.3","@semantic-release/git":"10.0.1","@types/debug":"4.1.12","ava":"5.3.1","c8":"9.1.0","knip":"4.6.0","semantic-release":"24.2.0","tsd":"0.31.2","xo":"0.56.0"},"xo":{"rules":{"unicorn/no-await-expression-member":"off","import/order":"off","object-curly-spacing":["error","always"]},"ignores":["lib/"]},"c8":{"include":["index.js"],"check-coverage":true,"reporter":["text","lcov"],"statements":100,"branches":100,"functions":100,"lines":100},"_id":"import-from-esm@2.0.0","gitHead":"a57041a3e398b3b319a98605503de1afc79a7b0b","types":"./index.d.ts","bugs":{"url":"https://github.com/sheerlox/import-from-esm/issues"},"homepage":"https://github.com/sheerlox/import-from-esm#readme","_nodeVersion":"22.12.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-YVt14UZCgsX1vZQ3gKjkWVdBdHQ6eu3MPU1TBgL1H5orXe2+jWD006WCPPtOuwlQm10NuzOW5WawiF1Q9veW8g==","shasum":"184eb9aad4f557573bd6daf967ad5911b537797a","tarball":"http://registry-npm.ywwl.com/import-from-esm/download/import-from-esm-2.0.0.tgz","fileCount":5,"unpackedSize":13305,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/import-from-esm@2.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBz0rACP7naMUwDnaL59Z8wOs8/99SsHYYH3jsvlh3I4AiEAmfip+Mu7dmz9z4KoYtlNSQcY+tfYZgmBnQ4Cc50Y+jQ="}],"size":5091,"noattachment":false,"key":"/import-from-esm/-/import-from-esm-2.0.0.tgz"},"_npmUser":{"name":"sheerlox","email":"me@sherlox.io"},"directories":{},"maintainers":[{"name":"sheerlox","email":"me@sherlox.io"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/import-from-esm_2.0.0_1735606035146_0.2946459527046372"},"_hasShrinkwrap":false,"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&label=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&server=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> 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<!-- prettier-ignore-start -->\n```js\n(fromDirectory, moduleId) => createRequire(path.resolve(fromDirectory, \"noop.js\"))(moduleId);\n```\n<!-- prettier-ignore-end -->\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","publish_time":1735606035316,"_cnpm_publish_time":1735606035316,"dist-tags":{"alpha":"1.4.0-alpha.1","latest":"2.0.0"}}