Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
The es-main npm package is a utility that helps determine if a module is being run directly or being imported as a module. This can be particularly useful for writing scripts that can be both executed directly and imported as part of a larger application.
Check if a module is the main module
This feature allows you to check if the current module is being run directly or being imported. The code sample demonstrates how to use the es-main package to print a message based on the module's execution context.
const esMain = require('es-main');
if (esMain(import.meta)) {
console.log('This module is being run directly.');
} else {
console.log('This module is being imported.');
}
The is-main package provides similar functionality by checking if the current module is the main module. It is simpler and has fewer dependencies compared to es-main.
The require-main-filename package helps you get the filename of the main module. While it doesn't directly check if a module is the main module, it can be used in conjunction with other logic to achieve similar results.
The built-in Node.js module object can be used to check if a module is the main module by comparing `require.main` with `module`. This is a more manual approach but doesn't require any additional packages.
Test if an ES module is run directly with Node.js. Acts as a replacement for require.main
.
import esMain from 'es-main';
if (esMain(import.meta)) {
// Module run directly.
}
It can be useful to have a module that is both imported from other modules and run directly. With CommonJS, it is possible to have a top-level condition that checks if a script run directly like this:
if (require.main === module) {
// Do something special.
}
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.
FAQs
Test if an ES module is run directly (require.main replacement)
We found that es-main 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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.