首页 > 解决方案 > npm 无法识别我的虚拟环境

问题描述

我正在制作一个使用烧瓶作为后端的反应应用程序,并且我遵循这篇文章指南,所以在这篇文章中他添加了一个自定义npm脚本来启动flask应用程序。我做了完全相同的事情,npm run start-api但我得到了以下错误 -

> weather-app@0.1.0 start-api C:\Users\Kakshipth\Documents\coding\webdev\weather-app
> cd api && venv/bin/flask run --no-debugger

'venv' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! weather-app@0.1.0 start-api: `cd api && venv/bin/flask run --no-debugger`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the weather-app@0.1.0 start-api script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Kakshipth\AppData\Roaming\npm-cache\_logs\2020-10-31T05_01_09_393Z-debug.log

是的,我仔细检查了我的虚拟环境的名称是否正确,并且正确。

这是我package.json指定脚本的文件-

{
  "name": "weather-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "materialize-css": "^1.0.0",
    "react": "^16.14.0",
    "react-dom": "^16.14.0",
    "react-scripts": "3.4.3",
    "venv": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "start-api": "cd api && venv/bin/flask run --no-debugger",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ],
    "proxy": "http://localhost:5000"
  }
}

任何帮助表示赞赏,谢谢

标签: node.jsjsonreactjsflasknpm

解决方案


为了在虚拟环境中运行,您需要先activate进入虚拟环境。

激活虚拟环境后尝试执行 npm 脚本

$ source venv/bin/activate
$ npm run start-api

推荐阅读