{"name":"import-from-esm","version":"1.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":">=16.20"},"scripts":{"test":"c8 ava && tsd","lint":"xo","lint:fix":"xo --fix"},"keywords":["require","resolve","path","module","from","like","import","path"],"devDependencies":{"@insurgentlab/conventional-changelog-preset":"^7.0.0","@semantic-release/changelog":"^6.0.3","@semantic-release/git":"^10.0.1","ava":"5.3.1","c8":"^8.0.1","semantic-release":"^22.0.5","tsd":"0.29.0","xo":"0.56.0"},"xo":{"rules":{"unicorn/no-await-expression-member":"off","object-curly-spacing":["error","always"]},"ignores":["lib/"]},"c8":{"include":["index.js"],"check-coverage":true,"reporter":["text","json"],"statements":95,"branches":85,"functions":100,"lines":95},"_id":"import-from-esm@1.0.0","gitHead":"0af90bcbcd62382b05f5e64f3af8bbb3b86648a8","types":"./index.d.ts","bugs":{"url":"https://github.com/sheerlox/import-from-esm/issues"},"homepage":"https://github.com/sheerlox/import-from-esm#readme","_nodeVersion":"18.18.0","_npmVersion":"10.2.0","dist":{"integrity":"sha512-9Rg+eQ3qC2sonLssXHTjmVmN3LBkh1PvWe7H3rCAFjR4vW/2mfiLQIwhwO9vvQvJUhM8DymRt0AneYFI6Fty2g==","shasum":"c8d32753c435be355a29dafb977d780664a83462","tarball":"http://registry-npm.ywwl.com/import-from-esm/download/import-from-esm-1.0.0.tgz","fileCount":12,"unpackedSize":56481,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEgRSWA+ykySq2qCNPOKA5UUb71QgPHaUa1NuskTZ/gmAiEAhdsdA8EP4UsIrtran2SNJfS98Srsr1ipaVjYVo53+bU="}],"size":14769,"noattachment":false,"key":"/import-from-esm/-/import-from-esm-1.0.0.tgz"},"_npmUser":{"name":"sheerlox","email":"me@sherlox.io"},"directories":{},"maintainers":[{"name":"sheerlox","email":"me@sherlox.io"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/import-from-esm_1.0.0_1697064603944_0.8835916561140771"},"_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":1697064604200,"_cnpm_publish_time":1697064604200,"dist-tags":{"alpha":"1.4.0-alpha.1","latest":"2.0.0"}}