【问题标题】:If `redux` is not in package.json or package-lock.json or node_modules, how come importing anything from 'redux' still works?如果 `redux` 不在 package.json 或 package-lock.json 或 node_modules 中,为什么从 'redux' 导入任何东西仍然有效?
【发布时间】:2020-06-26 14:56:48
【问题描述】:

我的项目中有一行:

import { createStore } from 'redux';

但是package.json 没有redux(它只有react-redux)。在package-lock.json 和文件夹node_modules 中也是如此。

此外,它不会全局安装,因为如果我这样做了

npm list -g redux

它显示它没有安装:

/usr/local/lib
└── (empty)

那么为什么我们仍然可以从redux 导入东西呢?

附注我仔细检查了 http://localhost:3000/static/js/0.chunk.js 并将redux 列为"../../../node_modules/redux/..."react-redux 列为"./node_modules/react-redux/... 所以我仔细检查了我的项目的两个级别,在我的主目录中,有一个@987654335 @文件夹......所以看起来它可以“上升”来寻找node_modules?那样会很危险吗?但另一方面,如果我们想开发几个实验性的 React 应用程序,而不重复相同的 node_modules,每个大约 200MB,是否有用?

【问题讨论】:

  • 我想如果你用 yarn 安装 yarn.lock 会有 redux 并且会告诉你它的位置。
  • @HMR 如果我做yarn install,那么我的yarn.lock 也只有react-redux@^7.2.0 但没有redux

标签: reactjs npm redux node-modules


【解决方案1】:

因为它是a peer dependency of react-redux,并且确实出现在node_modules/redux/index(在项目文件夹中)中,因为必须安装对等依赖项才能使用相关包。

"peerDependencies": {
    "react": "^16.8.3",
    "redux": "^2.0.0 || ^3.0.0 || ^4.0.0-0"
}

【讨论】:

  • 但在我的node_modules 中没有redux
  • 我很确定您没有找对地方,没有任何可生产的示例/屏幕截​​图无法帮助您。
  • $ ls node_modules/redux ls: node_modules/redux: No such file or directory
  • 为什么会在全局范围内?如答案中所述,它位于您的项目目录中的node_modules
  • 另外,我在自己的项目中检查了答案
猜你喜欢
  • 2018-07-05
  • 1970-01-01
  • 2020-02-10
  • 2018-10-06
  • 2019-07-31
  • 2012-05-01
  • 2020-09-24
  • 2020-11-19
  • 2019-06-07
相关资源
最近更新 更多