Created
June 29, 2021 17:32
-
-
Save lmammino/7dd898cf7ecfc6ec01e9cfaaa742e11e to your computer and use it in GitHub Desktop.
Modulino pattern in Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const x = require('./index.js') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import a from './index.mjs' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (require.main === module) { | |
| console.log('called directly'); | |
| } else { | |
| console.log('required as a module'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { fileURLToPath } from 'url' | |
| export default function () { } | |
| const isRunningDirectlyViaCLI = process.argv[1] === fileURLToPath(import.meta.url) | |
| if (isRunningDirectlyViaCLI) { | |
| console.log('called directly'); | |
| } else { | |
| console.log('required as a module'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment