DEV Community

Discussion on: Simple snippet to make Node's built in modules globally accessible

Collapse
 
jochemstoel profile image
Jochem Stoel

A couple of days ago I posted this snippet for easy access to Node's core modules but it wasn't easy enough because nobody wants to copy paste a snippet every time. That is arguably even more annoying than typing var fs = require('fs') every time.

I ended up publishing this module that wraps it to an elegant functional reactive oneliner. =p Just npm i internal.modules.

/* like this */
const { fs, childProcess, http } = require('internal.modules')
fs.writeFileSync('directories.txt', childProcess.execSync('dir'))

/* or even this */
const { mkdir, unlink } = require('internal.modules').fs 
unlink('directories.txt', error => error ? console.error(error) : console.log('file was deleted.'))