{"name":"esm-utils","version":"0.0.0","main":"index.js","license":"MIT","publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"_id":"esm-utils@0.0.0","_nodeVersion":"12.22.1","_npmVersion":"7.9.0","dist":{"integrity":"sha512-uHNhPodbPxWU6umAAQa3mmPZITcuBJPExMlv5r3EPSTjsOIEbGHbIfcqHkI/nTPtC7IcoLTFWa8unJ6UUw7mwg==","shasum":"ec339b1d0a1927f7f82bab47398b8ddbf095a08f","tarball":"http://registry-npm.ywwl.com/esm-utils/download/esm-utils-0.0.0.tgz","fileCount":1,"unpackedSize":196,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgfSghCRA9TVsSAnZWagAAHGYP/0pqNuErCLLfIB4oNFs3\nx0OuaWRU6+9cfJSf0RFYJTjd0kXG3Oci9ISjeU5Q8wPVN2jiMWJ00PD57My9\nf3I72t7a1pBWMRtvcn1kTuTIgzaf/38ytZSsoFZgKSJCTOMgSeIqpHv5PJzA\n0Uc65JibH51K4w7zUfJD9d6XEXHi+d6++ws/zULK24Wj89SrV/xi9KsLtOXS\nQWvqRDyDWv58zG1UEesTSCLslfgqp+D7glGw5W8UckKvDawKlFEhvFU5hsdS\nd52qZUNkmcPN4kSuKh4VxwQgy/SMj8om0VzWA1191wud1ZsKl87Dpd1FTa0b\nN3BJF7zC5MlBlJuytuyWYaHUR2C1XUzsDPi+DJrz8tHTvWeMfDbVX2flp+SM\nSYtn00MNzbBKxf+oZvvykh/E8pYcOcpsvhtFAID1wFT5/hbNO91hpoOA4rOA\nl2aQ8XH5V0NmgTbz3/WnAEUmIu4zfduWW2VVIm9N7PmeqTQYbj0GqqNboLV+\ndQmlShbzE/V8/UO+j9mhJFV5LnOHig+n6QWYoXBWlQVgDbbxDHgicLJKyXp5\ns7SxSMYXJkUL2jB8AtoC36eATmDd/AGDU3ZvBpzTn+shN4RoV8MTr6hVtJ7c\nB3lgUdEIE0eDOUiv1MwER6HATZ3+BWHxtFS7R6otwPIGJItuxphhZy0GKKUe\nda98\r\n=3rOD\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDBW9CpP/pHiTT1eQcEFQe4iGzDhQBxAf0zcNl3r6YGMwIhAMfXkkPfjZpW35wFvx6SEPAAI0FSaJnBwkKdx0cEoPmL"}],"size":226,"noattachment":false,"key":"/esm-utils/-/esm-utils-0.0.0.tgz"},"_npmUser":{"name":"fisker","email":"lionkay@gmail.com"},"directories":{},"maintainers":[{"name":"fisker","email":"lionkay@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/esm-utils_0.0.0_1618815008711_0.6087683235163293"},"_hasShrinkwrap":false,"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":1618815008868,"_cnpm_publish_time":1618815008868,"dist-tags":{"latest":"4.4.2"}}