{"name":"import-from-esm","version":"1.0.2","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 npm run test:all && npm run knip","test:all":"npm run test:ava && npm run test:fuzz && npm run test:tsd","test:ava":"ava tests/e2e.test.js","test:fuzz":"NODE_OPTIONS=--no-warnings ava tests/fuzz.test.js","test:tsd":"tsd --files tests/**/*.test-d.ts","knip":"knip","lint":"xo","lint:fix":"xo --fix"},"keywords":["require","resolve","path","module","from","like","import","path"],"devDependencies":{"@fast-check/ava":"1.1.6","@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","knip":"2.35.0","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","lcov"],"statements":95,"branches":85,"functions":100,"lines":95},"_id":"import-from-esm@1.0.2","gitHead":"b22ab1b33a5ca960a6d30d81d2fd0572d7c7b04f","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.2","_npmVersion":"10.2.0","dist":{"integrity":"sha512-3ffS93KSiD1sObPDvXAg5TeLGNEGJo6Sde3G6GIPWMZF/dn5tbMZ//0PyK2txckzaPgZymw3r3GE54zXmfCq/g==","shasum":"5a694d5020bbb2b81db699fe41d4b7538000528b","tarball":"http://registry-npm.ywwl.com/import-from-esm/download/import-from-esm-1.0.2.tgz","fileCount":12,"unpackedSize":56689,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFTeSDD7sYz93uBg85KvJ93An/dTDYsmSj1UblEuuzDqAiBxQH/MUWJtjGxj1QVnzflM4IqcAd6yF7Q/sDohACWyPg=="}],"size":14969,"noattachment":false,"key":"/import-from-esm/-/import-from-esm-1.0.2.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.2_1697879506179_0.778297101661334"},"_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":1697879506557,"_cnpm_publish_time":1697879506557,"dist-tags":{"alpha":"1.4.0-alpha.1","latest":"2.0.0"}}