{"name":"esm-utils","version":"4.4.1","author":{"url":"https://www.fiskercheung.com/","name":"fisker Cheung","email":"lionkay@gmail.com"},"license":"MIT","_id":"esm-utils@4.4.1","maintainers":[{"name":"fisker","email":"lionkay@gmail.com"}],"homepage":"https://github.com/fisker/esm-utils#readme","bugs":{"url":"https://github.com/fisker/esm-utils/issues"},"c8":{"reporter":["lcov","text"]},"ava":{"files":["!test/fixture.js"],"verbose":true},"dist":{"shasum":"51c45fd8a33f8af873e8a2836ab2060a194fb4d2","tarball":"http://registry-npm.ywwl.com/esm-utils/download/esm-utils-4.4.1.tgz","fileCount":12,"integrity":"sha512-l5ZGhZQTmTZ9XL+jLn6pfzYTiY8fsOyiCh6LO2PpDwka+ZepiObi9nZ2zesy1xabx8LBjLazM+TcN1w3NfDe9Q==","signatures":[{"sig":"MEYCIQDOw/3h/hdR/XHlfVkOBeyFXYYwZIvCAS6ov+y5kcSESQIhAJXE8nA4n1xKfdS1e07hhb2ttaVpmtk4+SyuWTWxvsF8","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":15874,"size":5543,"noattachment":false,"key":"/esm-utils/-/esm-utils-4.4.1.tgz"},"type":"module","exports":"./index.js","funding":"https://github.com/fisker/esm-utils?sponsor=1","gitHead":"cc09925567732c3e29537a75f7cf4fa1b1be9004","scripts":{"fix":"run-p --continue-on-error \"fix:*\"","dist":"run-p \"dist:*\"","lint":"run-p --continue-on-error \"lint:*\"","test":"ava","build":"run-p \"build:*\"","clean":"run-p \"clean:*\"","prepare":"husky install","release":"run-s lint build fix test dist","build:js":"echo no need","dist:npm":"release-it","clean:dist":"del-cli dist","fix:eslint":"yarn lint:eslint --fix","lint:eslint":"eslint .","fix:markdown":"yarn lint:markdown --fix","fix:prettier":"prettier . --write","lint:markdown":"markdownlint-cli2","lint:prettier":"prettier . --check","test-coverage":"c8 yarn test","fix:package-json":"sort-package-json \"package.json\" \"packages/*/package.json\"","lint:package-json":"yarn run fix:package-json --check"},"typings":"./index.d.ts","_npmUser":{"name":"fisker","email":"lionkay@gmail.com"},"repository":{"url":"git+https://github.com/fisker/esm-utils.git","type":"git"},"_npmVersion":"9.1.2","description":"Utilities you'll need when migrating to ESModule.","directories":{},"sideEffects":false,"_nodeVersion":"22.14.0","dependencies":{"url-or-path":"^2.6.1","import-meta-resolve":"^4.1.0"},"publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"_hasShrinkwrap":false,"packageManager":"yarn@4.9.1","devDependencies":{"c8":"10.1.3","ava":"6.2.0","husky":"9.1.7","eslint":"9.25.1","del-cli":"6.0.0","prettier":"3.5.3","release-it":"19.0.1","lint-staged":"15.5.1","npm-run-all2":"7.0.2","markdownlint-cli2":"0.17.2","sort-package-json":"3.0.0","@fisker/husky-config":"4.1.6","@fisker/eslint-config":"14.0.0","@fisker/prettier-config":"5.1.5","@fisker/eslint-config-ava":"3.0.3","cz-conventional-changelog":"3.3.0","@fisker/lint-staged-config":"3.3.5","@fisker/markdownlint-cli2-config":"0.0.3"},"_npmOperationalInternal":{"tmp":"tmp/esm-utils_4.4.1_1745864880677_0.8043832074911839","host":"s3://npm-registry-packages-npm-production"},"readme":"# esm-utils\n\n[![Build Status][github_actions_badge]][github_actions_link]\n[![Coverage][coveralls_badge]][coveralls_link]\n[![Npm Version][package_version_badge]][package_link]\n[![MIT License][license_badge]][license_link]\n\n[github_actions_badge]: https://img.shields.io/github/actions/workflow/status/fisker/esm-utils/continuous-integration.yml?branch=main&style=flat-square\n[github_actions_link]: https://github.com/fisker/esm-utils/actions?query=branch%3Amain\n[coveralls_badge]: https://img.shields.io/coveralls/github/fisker/esm-utils/main?style=flat-square\n[coveralls_link]: https://coveralls.io/github/fisker/esm-utils?branch=main\n[license_badge]: https://img.shields.io/npm/l/esm-utils.svg?style=flat-square\n[license_link]: https://github.com/fisker/esm-utils/blob/main/license\n[package_version_badge]: https://img.shields.io/npm/v/esm-utils.svg?style=flat-square\n[package_link]: https://www.npmjs.com/package/esm-utils\n\n> Utilities you'll need when migrating to ESModule.\n\n## Install\n\n```bash\nyarn add esm-utils\n```\n\n## Usage\n\n<!-- prettier-ignore -->\n```js\nimport createEsmUtils from 'esm-utils'\n\nconst {\n  dirname,\n  filename,\n  require,\n  importModule,\n  resolve,\n  readJson,\n  readJsonSync,\n} = createEsmUtils(import.meta)\n```\n\n```js\n/* Those named exports only accept absolute path or URL */\nimport {\n  importModule,\n  readJson,\n  loadJson,\n  readJsonSync,\n  loadJsonSync,\n} from 'esm-utils'\n```\n\n## API\n\n### `createEsmUtils(import.meta | URL | 'string')`\n\nReturns an `object` with the following properties:\n\n- `dirname` (alias `__dirname`)\n- `filename` (alias `__filename`)\n- `require`\n- `importModule` (alias `import`)\n- `resolve`\n- `readJson` (alias `loadJson`)\n- `readJsonSync` (alias `loadJsonSync`)\n\n**Please read [this note](#you-dont-need-dirname-and-filename) before you use `dirname` and `filename`**\n\nSync version of `readJson`.\n\n### `utils.importModule(string | URL, options?)`\n\nSame as `import()`, but accepts absolute path (on Windows, `import('C:\\\\foo.js')` error throws when pass a absolute path starts with a drive letter).\n\n#### `options.traceSyntaxError`\n\ntype: `boolean`\\\ndefault: `false`\n\nDue to [this Node.js issue](https://github.com/nodejs/modules/issues/471), Node.js does not emit the location of the syntax error in the error thrown in dynamic `import()`.\n\nWhen set `traceSyntaxError: true`, we'll try to get a better error message by running `node <file>` in a child process.\n\n### `utils.readJson(string | URL)`\n\nReturns `Promise<jsonObject>`.\n\n### `utils.readJsonSync(string | URL)`\n\nSync version of `utils.readJson`\n\n### `utils.resolve(string | URL)`\n\nPonyfill for [`import.meta.resolve`](https://nodejs.org/api/esm.html#esm_import_meta_resolve_specifier_parent).\n\nIf `import.meta.resolve` exits, use it directly, otherwise returns a simple wrapper of [import-meta-resolve](https://github.com/wooorm/import-meta-resolve).\n\n## Import json file\n\nWith [`Import Assertions`](https://github.com/tc39/proposal-import-assertions)\n\n```js\nimport foo from './foo.json' assert {type: 'json'}\n```\n\n```json\nawait import('./foo.json', {assert: {type: 'json'}})\n```\n\nWith `require`, like CommonJS\n\n```js\nimport createEsmUtils from 'esm-utils'\n\nconst {require} = createEsmUtils(import.meta)\nconst foo = require('./foo.json')\n```\n\nWith `readJson` or `readJsonSync`\n\n```js\nimport createEsmUtils from 'esm-utils'\n\nconst {readJson} = createEsmUtils(import.meta)\nconst foo = await readJson('./foo.json')\n```\n\n```js\nimport createEsmUtils from 'esm-utils'\n\nconst {readJsonSync} = createEsmUtils(import.meta)\nconst foo = readJsonSync('./foo.json')\n```\n\n## `importModule()`\n\nSame as `utils.importModule()`, but only accept absolute path or URL.\n\n## `readJson()` (alias `loadJson`)\n\nSame as `utils.readJson()`, but only accept absolute path or URL.\n\n## `readJsonSync()` (alias `loadJsonSync`)\n\nSame as `utils.readJsonSync()`, but only accept absolute path or URL.\n\n## You don't need `dirname` and `filename`\n\nThe `dirname` and `filename` supposed to be a quick solution when migrating to ES Modules. In most cases, you don't need them, because many APIs accept `URL` directly.\n\n<!-- prettier-ignore -->\n```js\n/* BAD */\nimport fs from 'node:fs/promises'\nimport path from 'node:path'\nimport createEsmUtils from 'esm-utils'\n\nconst {dirname} = createEsmUtils(import.meta)\nconst buffer = await fs.readFile(\n  path.join(dirname, './path/to/file')\n)\n```\n\n<!-- prettier-ignore -->\n```js\n/* GOOD */\nimport fs from 'node:fs/promises'\n\nconst buffer = await fs.readFile(\n  new URL('./path/to/file', import.meta.url)\n)\n```\n","publish_time":1745864880888,"_cnpm_publish_time":1745864880888,"dist-tags":{"latest":"4.4.2"}}