简体   繁体   English

Node.js 从 package.json 中安装 Python 依赖项

[英]Node.js install Python dependencies from within package.json

I'm building a Node.JS project that uses both Python and NPM dependency modules.我正在构建一个同时使用 Python 和 NPM 依赖模块的 Node.JS 项目。

The Node.JS modules are located within package.json and the python dependencies are inside a file requirements.txt . Node.JS 模块位于package.json中,python 依赖项位于文件requirements.txt中。

I would like to install all the dependency modules (Python and Node.JS) from within package.json by running npm install .我想通过运行npm installpackage.json中安装所有依赖模块(Python 和 Node.JS)。

Is this possible and how?这可能吗?如何实现?

Thanks in advance!提前致谢!

The files look like below.这些文件如下所示。

package.json:包.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:要求.txt:


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

You can define Commands to run within the "scripts" section of your package.json.您可以定义要在 package.json 的"scripts"部分中运行的命令。 Every script in there can be run with npm run [scriptname] .那里的每个脚本都可以使用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然后运行npm run install

Replace "dev": "npm install" with "dev": "npm install & pip install""dev": "npm install"替换为"dev": "npm install & pip install"

Add "preinstall" entry to scripts.向脚本添加“预安装”条目。

npm , yarn and pnpm will automatically execute preinstall script before installing dependencies and 'devDependencies`: npmyarnpnpm将在安装dependencies项和 'devDependencies` 之前自动执行preinstall脚本:

package.json:包.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 和 python 依赖项,只需运行:

$> npm install
  Installing Python Dependencies...

Also, there are other hook scripts in npm process, like "prepare", which might be useful.此外,npm 进程中还有其他钩子脚本,如“准备”,可能会有用。 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.脚本可以与... && npm run <script>链接,因此“脚本”部分可以组织成小的原子部分,并通过链接它们来构建。 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.我使用“脚本”作为项目的构建和部署活动知识库,不仅在 JS 中,甚至在纯 Python 项目中都取代了 make 文件功能。

It is also possible to hook "package.json" into python script, ie 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.也可以将“package.json”挂钩到 python 脚本中,即创建类似“build_project.py”脚本的东西(或者任何适合你的名称,我已经使用“make.py”和“build.py”来少打字)特定于项目,在那里添加所有与 python 相关的东西,并从中运行npm命令。 It may be more coding than using "scripts" section in "package.json".它可能比使用“package.json”中的“脚本”部分编码更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM