Skip to content

Instantly share code, notes, and snippets.

@lakshin
Last active November 24, 2017 18:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakshin/d2d6bbf37aa32bcd5007b615265db17e to your computer and use it in GitHub Desktop.
Save lakshin/d2d6bbf37aa32bcd5007b615265db17e to your computer and use it in GitHub Desktop.
Use multiple package versions in one node project
//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('chalk@1.0.0');
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