is there an virtual environment for node.js?

前端 未结 7 1879
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 08:06

I\'ve searched the wiki modules page, but I can\'t find anything similar to virtualenv (python) or rvm.

Anyone here separates node.js in their own env? I really don

7条回答
  •  误落风尘
    2020-12-07 08:46

    You don't always need to install dependencies globally. Usually it's recommended because then you can use the commands an npm packages provides, but if you install it locally (in the node_modules) directory, you can also use these commands, they only wind up in the node_modules/.bin/ directory, so you'll have to type node_modules/.bin/, which is annoying, but you can of course add this path to your PATH environment variable:

    export PATH=node_modules/.bin:$PATH
    

    Then you can just type and it works!

    There's actually an npm command that returns an absolute path to the .bin directory:

    $ npm bin
    /path/to/node_modules/.bin
    

    This command also works when you're in a subdirectory of the project, it will return the first node_modules/.bin directory it finds in it's parent directories.

    You can add this alias in your .bashrc to automatically add the .bin/ directory to your PATH:

    alias nodebin='export PATH=$(npm bin):$PATH'
    

    So when you're in a directory of a project that has a node_modules/ directory in the root, you can type nodebin and then you can use all the commands that are in the .bin/ directory!

提交回复
热议问题