There are some npm packages which I would like to install in a Python virtualenv. For example:
Up to now I only found the complicated way to get these installable in a virtualenv: Create a python package for them.
Is there no simpler way to get npm packages installed in a Python virtualenv?
To open the package manager, from Solution Explorer, right-click the npm node in your project. Next, you can search for npm packages, select one, and install by selecting Install Package.
Install Package Globally NPM installs global packages into /<User>/local/lib/node_modules folder. Apply -g in the install command to install package globally.
One solution is to install Node.js/npm inside your virtualenv, so here are the steps to get there: First off, activate your virtualenv, and: That's it! Now you can install npm packages directly to your virtualenv path with npm install -g {package}.
Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip. Unix/macOS python3 -m pip install --user virtualenv Windows py -m pip install --user virtualenv Creating a virtual environment¶
py -m pip install --user virtualenv Creating a virtual environment ¶ venv (for Python 3) and virtualenv (for Python 2) allow you to manage separate package installations for different projects. They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation.
They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation. When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments.
You can install NPM packages on your python virtuaenv using nodeenv.
source ./bin/activate
pip install nodeenv
nodeenv -p
To test if works:
npm install -g npm
npm -v
Sources:
https://pypi.org/project/nodeenv/
https://calvinx.com/2013/07/11/python-virtualenv-with-node-environment-via-nodeenv/
As @Josir suggests, I have used nodeenv
in the past but I had an issue when I wanted to have the node modules inside the venv
folder of the project as explained in this question.
In short putting a package.json
in venv
results in not being able to use npx ...
unless it is run from the venv
folder whereas putting package.json
in venv/lib
and running npm install
from there results in being able to use npx ...
from any folder in the project.
This is due to the NODE_PATH
environment variable being set to <myproject>/venv/lib/node_modules
.
I created a script to automate this which in substance does:
python -m venv venv
source venv/bin/activate
pip install requirements.txt
cp package.json venv/lib
cd venv/lib
nodeenv -p
npm install --no-optional
NPM and pip have nothing to do with each other, so you won't be able to install NPM packages inside a virtualenv.
However: NPM installs packages in ./node_modules
.
So if you created a virtualenv and installed npm modules inside it
virtualenv myproj
cd myproj
source bin/activate
npm install pdfjs-dist jquery-ui
you will end up with the node packages in myproj/node_modules
, which is as close as it gets to "installing NPM inside virtualenv".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With