Determines whether a Node file is a Module (`import`) or a Script (`require`)
Installations
npm install is-file-esm
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
14.8.0
NPM Version
6.14.7
Score
96.6
Supply Chain
98.6
Quality
74.7
Maintenance
100
Vulnerability
98.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
davidmarkclements
Download Statistics
Total Downloads
43,352,157
Last Day
24,495
Last Week
156,360
Last Month
1,272,792
Last Year
17,073,056
GitHub Statistics
11 Stars
1 Commits
3 Watching
1 Branches
1 Contributors
Bundle Size
96.83 kB
Minified
32.15 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.0
Package Id
is-file-esm@1.0.0
Size
5.25 kB
NPM Version
6.14.7
Node Version
14.8.0
Publised On
02 Oct 2020
Total Downloads
Cumulative downloads
Total Downloads
43,352,157
Last day
-25.5%
24,495
Compared to previous day
Last week
-44.7%
156,360
Compared to previous week
Last month
-5.4%
1,272,792
Compared to previous month
Last year
3%
17,073,056
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
3
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:
1{ 2 esm: Boolean, // true if file is a Module, false if it is a Script 3 type: String, // the determined package.json type, may be undefined, 'module', or 'commonjs' 4 extType: String, // the file extension type, may be 'c', 'm' or 'j' 5 path: String, // the input path 6 pkgPath: String // the path to the package.json from which the type was determined 7}
Awaitable (promise-based)
await isFileEsm(path) => Result
1 import isFileEsm from 'is-file-esm' 2 const { esm, path } = await isFileEsm('/path/to/file.js') 3 if (esm) console.log(`File ${path} is a Module`) 4 else console.log(`File ${path} is a Script`)
Callback-style
isFileEsm(path, cb(err, Result))
1 const isFileEsm = require('is-file-esm') 2 isFileEsm('/path/to/file.js', (err, result) => { 3 if (err) { 4 console.error(err) 5 return 6 } 7 if (result.esm) console.log(`File ${result.path} is a Module`) 8 else console.log(`File ${result.path} is a Script`) 9 })
Synchronous
isFileEsm.sync(path) => Result
1 import isFileEsm from 'is-file-esm' 2 const { esm, path } = isFileEsm.sync('/path/to/file.js') 3 if (esm) console.log(`File ${path} is a Module`) 4 else console.log(`File ${path} is a Script`)
Test
1npm 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
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/1 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3
/10
Last Scanned on 2025-01-06
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to is-file-esm
@types/is-file-esm
TypeScript definitions for is-file-esm
valid-module
Verify that file or package, be it remote, or local, is a valid ECMAScript Module
is-file-esm-ts
Determines if a Node file is a module.
check-esm
The module has JS functions to check all imports or required modules and check if the file is a ES Module or a CJS/ JS Module or Script