首页 > 解决方案 > node_module如何解决依赖关系

问题描述

假设我有一个xyz库(我使用安装的npm install xyz --save)和项目abc

node模块如何判断依赖是否应该在

abc/node_modules还是abc/node_modules/xyz/node_modules

标签: node.jsnpmnode-modules

解决方案


node_modules如果 "root" 中存在不兼容的版本,则将模块放置在嵌套中node_modules

iRohitBhatia 在对您问题的评论中所说的部分正确。但重要的因素不仅仅是版本 - 而是版本兼容性。


例子:

axios@0.21.1依赖于follow-redirects@^1.14.0

如果你package.json这样定义你的

{
    "dependencies": {
        "axios": "0.21.1"
    }
}

它安装在同一级别。

ls node_modules
axios
follow-redirects
ls node_modules/axios
# there is no nested `node_modules`

但是,如果您的 中有不兼容的版本follow-redirectsnode_modules它会将兼容版本安装到嵌套的node_modules/axios/node_modules.

{
    "dependencies": {
        "follow-redirects": "1.0.0",
        "axios": "0.21.1"
    }
}
ls node_modules
axios
debug
follow-redirects  # the non-compatible `1.0.0` version
ms
ls node_modules/axios/node_modules
follow-redirects  # the compatible version

推荐阅读