{"_id":"is-file-esm","_rev":"200739","name":"is-file-esm","description":"Determines whether a Node file is a Module (`import`) or a Script (`require`)","dist-tags":{"latest":"1.0.0"},"maintainers":[{"name":"davidmarkclements","email":"huperekchuno@googlemail.com"}],"time":{"modified":"2021-06-03T12:31:58.000Z","created":"2020-10-02T19:20:02.215Z","1.0.0":"2020-10-02T19:20:02.215Z"},"users":{},"author":{"name":"David Mark Clements","url":"@davidmarkclem"},"repository":{"type":"git","url":"git+https://github.com/davidmarkclements/is-file-esm.git"},"versions":{"1.0.0":{"name":"is-file-esm","version":"1.0.0","description":"Determines whether a Node file is a Module (`import`) or a Script (`require`)","main":"index.js","scripts":{"test":"tap -R classic --100","ci":"npm test"},"keywords":["esm","cjs","package.json","module","ecmascript modules","native modules","native ecmascript modules"],"author":{"name":"David Mark Clements","url":"@davidmarkclem"},"license":"MIT","engineStrict":true,"engine":{"node":">= 12.4.0"},"devDependencies":{"nonsynchronous":"^1.2.0","standard":"^14.3.4","tap":"^14.10.8"},"dependencies":{"read-pkg-up":"^7.0.1"},"directories":{"test":"test"},"repository":{"type":"git","url":"git+https://github.com/davidmarkclements/is-file-esm.git"},"bugs":{"url":"https://github.com/davidmarkclements/is-file-esm/issues"},"homepage":"https://github.com/davidmarkclements/is-file-esm#readme","gitHead":"83ec13a445422820f4047801764a26f247293411","_id":"is-file-esm@1.0.0","_nodeVersion":"14.8.0","_npmVersion":"6.14.7","dist":{"shasum":"987086b0f5a5318179e9d30f4f2f8d37321e1b5f","size":5377,"noattachment":false,"key":"/is-file-esm/-/is-file-esm-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/is-file-esm/download/is-file-esm-1.0.0.tgz"},"maintainers":[{"name":"davidmarkclements","email":"huperekchuno@googlemail.com"}],"_npmUser":{"name":"davidmarkclements","email":"david.mark.clements@gmail.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/is-file-esm_1.0.0_1601666402014_0.4962500696815022"},"_hasShrinkwrap":false,"publish_time":1601666402215,"_cnpm_publish_time":1601666402215}},"readme":"# is-file-esm\n\n> Determines whether a Node file is a Module (`import`) or a Script (`require`)\n\n## Algorithm\n\nDetermining the module system of a file comes from three inputs, the `type` field \nof the closest `package.json` to that file, the file extension (`.js`, `.mjs` or `.cjs`)\nand the lesser know `--input-type` command-line flag. The latter only applies to \ndyamic input (via STDIN, `--eval` or `--print` flags) so is not considered with this library.\n\nSo to determine whether a file is an esm file (e.g. native EcmaScript modules) or not,\nwe use the following procedure:\n\n```\nread package.json for \"type\" field,\n  if type is \"module\"\n    if answer.mjs -> module \n    if answer.js -> module\n    if answer.cjs -> script\n  if type is \"commonjs\"\n    if answer.mjs -> module   \n    if answer.js -> script\n    if answer.cjs -> script\n  if type is not set\n    if answer.mjs -> module\n    if answer.js -> script\n    if answer.cjs -> script\n```\n\n## API\n\nThe `is-file-esm` module provides synchronous, awaitable (promise-based) and callback based APIs. \n\nIn each case the Result object has the following shape: \n\n```js\n{\n  esm: Boolean, // true if file is a Module, false if it is a Script\n  type: String,  // the determined package.json type, may be undefined, 'module', or 'commonjs'\n  extType: String, // the file extension type, may be 'c', 'm' or 'j'\n  path: String,  // the input path\n  pkgPath: String // the path to the package.json from which the type was determined\n}\n```\n\n### Awaitable (promise-based)\n\n#### `await isFileEsm(path) => Result`\n\n```js\n  import isFileEsm from 'is-file-esm'\n  const { esm, path } = await isFileEsm('/path/to/file.js')\n  if (esm) console.log(`File ${path} is a Module`)\n  else console.log(`File ${path} is a Script`)\n```\n\n### Callback-style\n\n#### `isFileEsm(path, cb(err, Result))`\n\n```js\n  const isFileEsm = require('is-file-esm')\n  isFileEsm('/path/to/file.js', (err,  result) => {\n    if (err) {\n      console.error(err)\n      return\n    }\n    if (result.esm) console.log(`File ${result.path} is a Module`)\n    else console.log(`File ${result.path} is a Script`)\n  })\n```\n\n### Synchronous\n\n#### `isFileEsm.sync(path) => Result`\n\n```js\n  import isFileEsm from 'is-file-esm'\n  const { esm, path } = isFileEsm.sync('/path/to/file.js')\n  if (esm) console.log(`File ${path} is a Module`)\n  else console.log(`File ${path} is a Script`)\n```\n\n### Test\n\n```sh\nnpm test\n```\n\n```\ntest/index.js ..................................... 213/213\ntotal ............................................. 213/213\n\n  213 passing (927.584ms)\n\n  ok\n----------|----------|----------|----------|----------|-------------------|\nFile      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |\n----------|----------|----------|----------|----------|-------------------|\nAll files |      100 |      100 |      100 |      100 |                   |\n index.js |      100 |      100 |      100 |      100 |                   |\n----------|----------|----------|----------|----------|-------------------|\n```\n\n### License\n\nMIT","_attachments":{},"homepage":"https://github.com/davidmarkclements/is-file-esm#readme","bugs":{"url":"https://github.com/davidmarkclements/is-file-esm/issues"},"license":"MIT"}