Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 1.07 KB

2016-01-17-nodejs-run-a-module-if-it-is-not-required.md

File metadata and controls

32 lines (26 loc) · 1.07 KB
layout title tip-number tip-username tip-username-profile tip-tldr redirect_from categories
post
Node.js - Run a module if it is not `required`
17
odsdq
In node, you can tell your program to do two different things depending on whether the code is run from `require('./something.js')` or `node something.js`. This is useful if you want to interact with one of your modules independently.
/en/nodejs-run-a-module-if-it-is-not-required/
en
javascript

In node, you can tell your program to do two different things depending on whether the code is run from require('./something.js') or node something.js. This is useful if you want to interact with one of your modules independently.

if (!module.parent) {
    // ran with `node something.js`
    app.listen(8088, function() {
        console.log('app listening on port 8088');
    })
} else {
    // used with `require('/.something.js')`
    module.exports = app;
}

See the documentation for modules for more info.