首页 > 解决方案 > 如何添加依赖来响应?

问题描述

我正在尝试将以下库添加到反应项目中:

https://github.com/rndme/download

我做了以下事情:

yarn add https://github.com/rndme/download

并得到了这样的回应:

info No lockfile found.
[1/4]   Resolving packages...
[2/4]   Fetching packages...
[3/4]   Linking dependencies...
[4/4]   Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ downloadjs@1.4.8
info All dependencies
└─ downloadjs@1.4.8
✨  Done in 2.19s.

然后我更新了 package.json 以包含它:

  "dependencies": {
    "bootstrap": "4.1.3",
    "react": "^16.6.0",
    "react-cookie": "3.0.4",
    "react-dom": "^16.6.0",
    "react-router-dom": "4.3.1",
    "react-scripts": "2.1.0",
    "reactstrap": "6.5.0",
    "downloadjs": "1.4.8"
  },

并添加了一些代码来调用它:

  async downloadFile(file, url) {
    const res = await fetch(url);
    const blob = res.blob();

    // from downloadjs it will download your file
    download(blob, file, "text/plain");  
  }

但是,它不会编译:

Failed to compile
./src/LicenseList.js
  Line 64:  'download' is not defined  no-undef

知道为什么吗?

标签: javascriptreactjsyarnpkg

解决方案


如果您使用纱线,您只需要编写yarn add mydepencynameyarn add -D mydependencyname添加 devDependency (也就是说“仅在开发阶段有用”。

您将在 npm.com 上找到正确的依赖项名称。要更新依赖项,只需键入yarn upgrade-interactive --latest,然后按空格键选择要更新的包。


推荐阅读