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?
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".
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.