if (require.main === module) {
console.log('called directly');
} else {
console.log('required as a module');
}
See documentation for this here: https://nodejs.org/docs/latest/api/modules.html#modules_accessing_the_main_module
More Related Contents:
- How to deal with cyclic dependencies in Node.js
- The difference between “require(x)” and “import x”
- How to make node.js require absolute? (instead of relative)
- How to remove module after “require” in node.js?
- What is the difference between require(‘mypackage.js’) and require(‘mypackage’)?
- How does require work with new operator in node.js?
- How can I make Node.js ‘require’ absolute? (instead of relative)
- Unable to get output after passing functions in node js program? [closed]
- Is there a way to make npm install (the command) to work behind proxy?
- How do you turn a Mongoose document into a plain object?
- How to edit a node module installed via npm?
- How can I set cookie in node js using express framework?
- Setting expiry time for a collection in mongodb using mongoose
- HAProxy + WebSocket Disconnection
- Create a HOC (higher order component) for authentication in Next.js
- node.js – request – How to “emitter.setMaxListeners()”?
- Proxy with express.js
- How do I host multiple Node.js sites on the same IP/server with different domains?
- Redirect all trailing slashes globally in express
- Read remote file with node.js (http.get)
- How to use http.client in Node.js if there is basic authorization
- What TypeScript configuration produces output closest to Node.js 14 capabilities?
- How to fix npm vulnerabilities manually?
- How to open a create-react-app from another computer connected to the same network?
- How to enable BLOB-logging for a Node.js Api App on Azure?
- How to get console.log line numbers shown in Nodejs?
- Gatsby Failed Build – error “window” is not available during server side rendering
- ISODate is not defined
- Firestore Add value to array field
- Can I access a cookie from Socket.io?