Skip to content

Instantly share code, notes, and snippets.

@tomasevich
Created September 25, 2018 08:00
Show Gist options
  • Save tomasevich/1675eee92fd4b25265f728307f48ce93 to your computer and use it in GitHub Desktop.
Save tomasevich/1675eee92fd4b25265f728307f48ce93 to your computer and use it in GitHub Desktop.
Auto-install modules :)
'use strict'
const { exec } = require ('child_process')
try {
const express = require ('express')
const ejs = require ('ejs')
var app = express ()
app.listen (8080)
} catch (e) {
if (/^Cannot find module \'([a-z]{0,20})\'$/i.exec (e.message)) {
let name = /^Cannot find module \'([a-z]{0,20})\'$/i.exec (e.message)[1]
console.log (`install {${name}} module`)
exec (`npm i ${name} --save`)
}
}
@tomasevich
Copy link
Author

tomasevich commented Sep 25, 2018

or auto load :)

'use strict'

for (let mod in require ('./package').dependencies) {
	global[mod] = require (mod)
}

try {
	var app = express ()
		app.listen (8080)
} catch (e) {
	
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment