Skip to content

Instantly share code, notes, and snippets.

@debrouwere
Created December 17, 2010 11:53
Show Gist options
  • Save debrouwere/744830 to your computer and use it in GitHub Desktop.
Save debrouwere/744830 to your computer and use it in GitHub Desktop.
virtual environments in npm, the node package manager (rough sketch)
# run this in a test directory using a regular bash prompt to get an idea of how virtual environments would work, roughly
alias npm-env-init="mkdir ./env; mkdir ./env/packages; echo 'root = ./env/packages' > ./env/.npmrc; echo 'Created a new virtual environment for node packages in ./env'"
alias npm-env-workon="export npm_config_userconfig=./env/.npmrc; export NODE_PATH=./env/packages;"
alias npm-env-ls="nvp workon; nvm ls active;"
# we make a virtual environment
npm-env-init
# we switch to that virtual environment
npm-env-workon
# we install a package
npm install underscore
# ... which is installed into the virtual environment
ls env/packages/underscore
# ... and entirely isolated from anything that's installed sitewide,
# which we can check because nvm ls active will only list underscore
# and nothing else
npm ls active
# besides the obvious advantage of being able to control what packages
# and which versions get used per project, which arguably can be accomplished
# with an npm bundle, true virtual environments could enable the following
# nifty behavior:
npm ls active > requirements.txt
cat requirements.txt | xargs -I % npm install %
# The advantage over bundles being that this avoids having to specify
# dependencies manually, and that you don't have to create a package.json
# file if your project isn't really a package anyway.
@isaacs
Copy link

isaacs commented Mar 16, 2023

By the way, if you want something a lot more like virtualEnv, check out https://github.com/isaacs/nave

@pzhlkj6612
Copy link

By the way, if you want something a lot more like virtualEnv, check out https://github.com/isaacs/nave

@isaacs , thanks. I'll try it later.

Maybe you can add a link to nave into your answer on StackOverflow :-)

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