Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
is-file-esm
Advanced tools
Determines whether a Node file is a Module (`import`) or a Script (`require`)
Determines whether a Node file is a Module (
import
) or a Script (require
)
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
The is-file-esm
module provides synchronous, awaitable (promise-based) and callback based APIs.
In each case the Result object has the following shape:
{
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
}
await isFileEsm(path) => Result
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`)
isFileEsm(path, cb(err, Result))
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`)
})
isFileEsm.sync(path) => Result
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`)
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 | |
----------|----------|----------|----------|----------|-------------------|
MIT
FAQs
Determines whether a Node file is a Module (`import`) or a Script (`require`)
The npm package is-file-esm receives a total of 306,358 weekly downloads. As such, is-file-esm popularity was classified as popular.
We found that is-file-esm demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.