Comparing version 1.0.2 to 1.1.0
17
main.js
@@ -0,5 +1,10 @@ | ||
import path from 'path'; | ||
import process from 'process'; | ||
import {fileURLToPath} from 'url'; | ||
import process from 'process'; | ||
import path from 'path'; | ||
/** | ||
* Strip the extension from a filename if it has one. | ||
* @param {string} name A filename. | ||
* @return {string} The filename without a path. | ||
*/ | ||
export function stripExt(name) { | ||
@@ -14,3 +19,9 @@ const extension = path.extname(name); | ||
export default function(meta) { | ||
/** | ||
* Check if a module was run directly with node as opposed to being | ||
* imported from another module. | ||
* @param {ImportMeta} meta The `import.meta` object. | ||
* @return {boolean} The module was run directly with node. | ||
*/ | ||
export default function esMain(meta) { | ||
const modulePath = fileURLToPath(meta.url); | ||
@@ -17,0 +28,0 @@ |
{ | ||
"name": "es-main", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Test if an ES module is run directly (require.main replacement)", | ||
@@ -12,10 +12,16 @@ "main": "main.js", | ||
"files": [ | ||
"main.js" | ||
"main.js", | ||
"main.d.ts", | ||
"main.d.ts.map" | ||
], | ||
"scripts": { | ||
"pretest": "eslint .", | ||
"lint": "eslint .", | ||
"pretest": "npm run lint", | ||
"generate-types": "npx tsc -declaration --declarationMap --emitDeclarationOnly --noEmit false", | ||
"preversion": "npm run generate-types", | ||
"test:with-extension": "node test.js", | ||
"test:without-extension": "node test", | ||
"test:without-node": "./test.js", | ||
"test": "NODE_NO_WARNINGS=1 npm-run-all test:*" | ||
"test:types": "npx tsc --noEmit", | ||
"test": "npm-run-all test:*" | ||
}, | ||
@@ -29,11 +35,14 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-eslint": "^10.0.3", | ||
"eslint": "^6.8.0", | ||
"eslint-config-tschaub": "^13.1.0", | ||
"npm-run-all": "^4.1.5" | ||
"@types/node": "^18.0.0", | ||
"eslint": "^8.18.0", | ||
"eslint-config-tschaub": "^14.0.0", | ||
"npm-run-all": "^4.1.5", | ||
"typescript": "^4.7.4" | ||
}, | ||
"eslintConfig": { | ||
"extends": "tschaub", | ||
"parser": "babel-eslint" | ||
"rules": { | ||
"jsdoc/check-examples": "off" | ||
} | ||
} | ||
} |
@@ -25,2 +25,2 @@ # es-main | ||
With ES modules in Node.js, `require.main` is not available. Other alternatives like `process.mainModule` and `module.parent` are also not defined for ES modules. In the future, there may be an alternative way to do this check (e.g. `import.meta.main` or a special `main` export). Until then, this package provides a workaround. | ||
With ES modules in Node.js, `require.main` is [not available](https://nodejs.org/dist/latest-v14.x/docs/api/esm.html#esm_no_require_exports_module_exports_filename_dirname). Other alternatives like `process.mainModule` and `module.parent` are also not defined for ES modules. In the future, there may be [an alternative way](https://github.com/nodejs/modules/issues/274) to do this check (e.g. `import.meta.main` or a special `main` export). Until then, this package provides a workaround. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3756
5
43
5