{"name":"import-from-esm","version":"1.3.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":"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":"cross-env NODE_OPTIONS=--no-warnings ava tests/fuzz.test.js","test:fuzz:regression":"cross-env NODE_OPTIONS=--no-warnings 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.1.6","@insurgent/export-map-test":"1.0.0","@insurgent/json-index-test":"1.0.0","@insurgent/json-main-test":"1.0.0","@insurgentlab/conventional-changelog-preset":"7.0.0","@semantic-release/changelog":"6.0.3","@semantic-release/git":"10.0.1","@types/debug":"4.1.12","ava":"5.3.1","c8":"8.0.1","cross-env":"7.0.3","knip":"2.41.2","semantic-release":"22.0.7","tsd":"0.29.0","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":95,"branches":90,"functions":100,"lines":95},"_id":"import-from-esm@1.3.2","gitHead":"df8e9da5050db954c59dd9b93744497bffe46de7","types":"./index.d.ts","bugs":{"url":"https://github.com/sheerlox/import-from-esm/issues"},"homepage":"https://github.com/sheerlox/import-from-esm#readme","_nodeVersion":"20.9.0","_npmVersion":"10.2.3","dist":{"integrity":"sha512-5IeOH/8/fVZq/US/OMDwAHgbAUXPZ7Z/KUM6pvCXeuCPZfrZSxkzEPkfoDXUliR9kMrtX/H+wJOZCq0+MH158A==","shasum":"d6c8984a1656045c7744ac397ff28e846924b24b","tarball":"http://registry-npm.ywwl.com/import-from-esm/download/import-from-esm-1.3.2.tgz","fileCount":5,"unpackedSize":12925,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFKdRWfPMAscbflJFlcnf0ILsV+cJybnK1zjzwpuSMvfAiEA0HCpZsVREYdzePaLnNBKVmnhVCEfDlvE0tyeZHID5n4="}],"size":5033,"noattachment":false,"key":"/import-from-esm/-/import-from-esm-1.3.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.3.2_1700354726590_0.6207768143766825"},"_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":1700354726832,"_cnpm_publish_time":1700354726832,"dist-tags":{"alpha":"1.4.0-alpha.1","latest":"2.0.0"}}