Node.js install Python dependencies from within package.json

Question:

I’m building a Node.JS project that uses both Python and NPM dependency modules.

The Node.JS modules are located within package.json and the python dependencies are inside a file requirements.txt.

I would like to install all the dependency modules (Python and Node.JS) from within package.json by running npm install.

Is this possible and how?

Thanks in advance!

The files look like below.

package.json:


{
  "name": "app",
  "version": "1.0.0",
  "description": "Trial app",
  "main": "bin/index.js",
  "scripts": {
    "dev": "npm install",
    "start": "node app.js",
    "test": "jest --forceExit "
  },
  "keywords": [
    "AI",
    "Blockchain",
    "Decentralized"
  ],
  "dependencies": {
    "peerjs": "^1.3.2",
    "redis": "^3.1.2",
    "socket.io": "^4.1.2",
    "socket.io-client": "^4.1.2",
    "wrtc": "^0.4.7"
  },
  "devDependencies": {
    "@babel/core": "^7.16.7",
    "supertest": "^6.1.6"
  }
}

requirements.txt:


Django==2.2.21
djangorestframework==3.7.7
django-rest-swagger
coreapi

Asked By: Vakindu

||

Answers:

Replace "dev": "npm install" with "dev": "npm install & pip install"

Answered By: Vladyslav Yukhanov

You can define Commands to run within the "scripts" section of your package.json. Every script in there can be run with npm run [scriptname].

So you could do (&& runs another command after the first one)

"scripts": {
    "install": "npm install && pip -r requirements.txt",
    "dev": "npm install",
    "start": "node app.js",
    "test": "jest --forceExit "
  }

And run npm run install

Answered By: nonnial

Add "preinstall" entry to scripts.

npm, yarn and pnpm will automatically execute preinstall script before installing dependencies and ‘devDependencies`:

package.json:

{
  "scripts": {
    "preinstall": "echo 'Installing Python Dependencies...' && pip install -r requirements.txt && pip install -r subproject/requirements.txt"
  },
  ...
}

To install both npm and python dependencies, just run:

$> npm install
  Installing Python Dependencies...

Also, there are other hook scripts in npm process, like "prepare", which might be useful. And scripts can be chained with ... && npm run <script>, so the "scripts" section can be organized into small atomic ones and built up by chaining them. I use "scripts" as a project’s build and deploy active knowledgebase, replacing make file functionality not only in JS, but even in pure Python projects.

It is also possible to hook "package.json" into python script, i.e. create something like "build_project.py" script (or whatever name that works for you, I’ve used "make.py" and "build.py" for less typing) specific to the project, add all python-related stuff there, and run the npm commands from it. It may be more coding than using "scripts" section in "package.json".

Answered By: iva2k
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.