Moving node_modules location under a Python venv

2019-08-19 03:35发布

问题:

I have a Python venv to develop a Django application and I use babel to transpile my javascript.

Currently, I install node.js with nodeenv -p whilst working under my Python venv.

Everything works fine if I have the node_modules and package.json folder at the root of my project, but it is messing up the project folder structure.

I would rather have node_modules under venv:

project_folder
   |
   |- venv
         |- node_modules
         package.json

or even:

project_folder
   |
   |- venv
         |- node
               |- node_modules
               package.json

If I put my package.json in venv and execute npm install from there, npx babel only works if I am in venv, not a the root of the project.

Running npx babel from the project's root directory gives me:

npx: installed 1 in 0.655s
You have mistakenly installed the `babel` package, which is a no-op in Babel 6.
Babel's CLI commands have been moved from the `babel` package to the `babel-cli` package.

    npm uninstall -g babel
    npm install --save-dev babel-cli

See http://babeljs.io/docs/usage/cli/ for setup instructions.

How can I have node_modules under the venv folder and be able to use npx babel from anywhere in the project folder?

This question is related but I can't see how to adapt the answer to nodeenv.

回答1:

I eventually figured out that NODE_PATH is set to <myproject>/venv/lib/node_modules.

If I put my package.json file in venv/lib/ then run npm install from this directory, I can use npx babel from any folder in the project.