# is-file-esm

> Determines whether a Node file is a Module (`import`) or a Script (`require`)

Latest version **1.0.0** (published 2020-10-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install is-file-esm
pnpm add is-file-esm
yarn add is-file-esm
bun add is-file-esm
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-10-02 |
| First published | 2020-10-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/is-file-esm) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 32.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | David Mark Clements |
| Maintainers | davidmarkclements |
| Keywords | esm, cjs, package.json, module, ecmascript modules, native modules, native ecmascript modules |

## Links

- npm: https://www.npmjs.com/package/is-file-esm
- Repository: https://github.com/davidmarkclements/is-file-esm
- Homepage: https://github.com/davidmarkclements/is-file-esm#readme
- Issues: https://github.com/davidmarkclements/is-file-esm/issues
- npm.io page: https://npm.io/package/is-file-esm

## Dependencies (1)

- [read-pkg-up](https://npm.io/package/read-pkg-up.md) ^7.0.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.0 (latest) — 2020-10-02

## README

# is-file-esm

> Determines whether a Node file is a Module (`import`) or a Script (`require`)

## Algorithm

Determining the module system of a file comes from three inputs, the `type` field 
of the closest `package.json` to that file, the file extension (`.js`, `.mjs` or `.cjs`)
and the lesser know `--input-type` command-line flag. The latter only applies to 
dyamic input (via STDIN, `--eval` or `--print` flags) so is not considered with this library.

So to determine whether a file is an esm file (e.g. native EcmaScript modules) or not,
we use the following procedure:

```
read package.json for "type" field,
  if type is "module"
    if answer.mjs -> module 
    if answer.js -> module
    if answer.cjs -> script
  if type is "commonjs"
    if answer.mjs -> module   
    if answer.js -> script
    if answer.cjs -> script
  if type is not set
    if answer.mjs -> module
    if answer.js -> script
    if answer.cjs -> script
```

## API

The `is-file-esm` module provides synchronous, awaitable (promise-based) and callback based APIs. 

In each case the Result object has the following shape: 

```js
{
  esm: Boolean, // true if file is a Module, false if it is a Script
  type: String,  // the determined package.json type, may be undefined, 'module', or 'commonjs'
  extType: String, // the file extension type, may be 'c', 'm' or 'j'
  path: String,  // the input path
  pkgPath: String // the path to the package.json from which the type was determined
}
```

### Awaitable (promise-based)

#### `await isFileEsm(path) => Result`

```js
  import isFileEsm from 'is-file-esm'
  const { esm, path } = await isFileEsm('/path/to/file.js')
  if (esm) console.log(`File ${path} is a Module`)
  else console.log(`File ${path} is a Script`)
```

### Callback-style

#### `isFileEsm(path, cb(err, Result))`

```js
  const isFileEsm = require('is-file-esm')
  isFileEsm('/path/to/file.js', (err,  result) => {
    if (err) {
      console.error(err)
      return
    }
    if (result.esm) console.log(`File ${result.path} is a Module`)
    else console.log(`File ${result.path} is a Script`)
  })
```

### Synchronous

#### `isFileEsm.sync(path) => Result`

```js
  import isFileEsm from 'is-file-esm'
  const { esm, path } = isFileEsm.sync('/path/to/file.js')
  if (esm) console.log(`File ${path} is a Module`)
  else console.log(`File ${path} is a Script`)
```

### Test

```sh
npm test
```

```
test/index.js ..................................... 213/213
total ............................................. 213/213

  213 passing (927.584ms)

  ok
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.js |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
```

### License

MIT

---
_Source: https://npm.io/package/is-file-esm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
