Last active
November 24, 2017 18:13
-
-
Save lakshin/d2d6bbf37aa32bcd5007b615265db17e to your computer and use it in GitHub Desktop.
Use multiple package versions in one node project
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
//server.js | |
const http = require('http'); | |
//const niv = require('npm-install-version'); | |
const chalkv2 = require('chalk'); | |
const multiRequire = require('./multiRequire'); | |
const chalkv1 = multiRequire().multiRequire('chalk','1.0.0'); | |
console.log('chalk v1'+chalkv1.blue(chalkv1.version)); | |
console.log('chalk v2'+chalkv2.blue(chalkv2.version)); | |
const hostname = '127.0.0.1'; | |
const port = 3000; | |
//niv.install('[email protected]'); | |
const server = http.createServer((req, res) => { | |
res.statusCode = 200; | |
res.setHeader('Content-Type', 'text/plain'); | |
res.end('Hello World\n'); | |
}); | |
server.listen(port, hostname, () => { | |
console.log(`Server running at http://${hostname}:${port}/`); | |
}); | |
//multirequire.js | |
(function(){ | |
var fs = require('fs'); | |
module.exports = function(){ | |
var path = "./node_modules"; | |
function multiRequire(module,version){ | |
if (fs.existsSync(path+'/'+module+'@'+version)){ | |
console.log('exist'); | |
return require(path+'/'+module+'@'+version); | |
}else{ | |
console.log('not exist'); | |
return null; | |
} | |
} | |
return { | |
multiRequire: multiRequire | |
} | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment