Web

Ag-grid unable to use xxx as package ag-grid-enterprise is not present

Posted by Kerwen Blog on February 14, 2023

最近在对产品的package进行升级,将ag-grid 升到28.2.0之后爆出了一堆的问题,其中一个是在debug console里有如下warnning提示。 产品使用了侧边栏,需要用到@ag-grid-enterprise/column-tool-panel@ag-grid-enterprise/filter-tool-panel, 但这两个module注册之后也无法打开侧边栏,debug console有warning。

img

研究之后发现跟package的版本有关, 在package.json里我将所有ag-grid版本都指定为28.2.0,打开package-lock.json,搜索@ag-grid-enterprise/column-tool-panel@ag-grid-enterprise/filter-tool-panel,发现它俩都依赖@ag-grid-enterprise/side-bar

1
2
3
4
5
6
7
8
9
10
11
"node_modules/@ag-grid-enterprise/column-tool-panel": {
  "version": "28.2.0",
  "resolved": "https://registry.npmjs.org/@ag-grid-enterprise/column-tool-panel/-/column-tool-panel-28.2.0.tgz",
  "integrity": "sha512-ApgrgSTRjPpkHz11BAGSJ0kkpkFtDDiJ7xz8hzXS4Pth9XifbQdgweG2JY1xZNH1kJP525qgQv2o2Sxfehb+xQ==",
  "dependencies": {
    "@ag-grid-community/core": "~28.2.0",
    "@ag-grid-enterprise/core": "~28.2.0",
    "@ag-grid-enterprise/row-grouping": "~28.2.0",
    "@ag-grid-enterprise/side-bar": "~28.2.0"
  }
},

将这个依赖加到package.json里,重新npm install,还是无法解决问题。继续看package-lock.json,搜索@ag-grid-enterprise/side-bar, 会发现它依赖 @ag-grid-community/core 28.2.1@ag-grid-enterprise/core 28.2.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    "node_modules/@ag-grid-enterprise/side-bar": {
        "version": "28.2.0",
        "resolved": "https://registry.npmjs.org/@ag-grid-enterprise/side-bar/-/side-bar-28.2.0.tgz",
        "integrity": "sha512-4n7aDrMjGFg3qcNoPWIC8gDvCAMYUMuqhrjYOtsapJ+FCTENhNEGwMLK2GI+nyaiCQqq7PAhPoDN6hLVwy5xJQ==",
        "dependencies": {
            "@ag-grid-community/core": "~28.2.0",
            "@ag-grid-enterprise/core": "~28.2.0"
        }
    },
    "node_modules/@ag-grid-enterprise/side-bar/node_modules/@ag-grid-community/core": {
        "version": "28.2.1",
        "resolved": "https://registry.npmjs.org/@ag-grid-community/core/-/core-28.2.1.tgz",
        "integrity": "sha512-qGhqZhY8KbPlr3cJBVkNjGKu+cOyFc8IGvPcvlm0pjNC9cdxO/ct9Sa1Dg+k3ACCEroMEt1apBvnoOgurpvewQ=="
        },
    "node_modules/@ag-grid-enterprise/side-bar/node_modules/@ag-grid-enterprise/core": {
        "version": "28.2.1",
        "resolved": "https://registry.npmjs.org/@ag-grid-enterprise/core/-/core-28.2.1.tgz",
        "integrity": "sha512-VzmD03YdyokPR8YQ3BOfJCSn2AwIRpFmu0U6/Smheyjlw84cFjdqI9Ef1GXLEUcKGB9dwnQESEfq9gIckwHcug==",
        "dependencies": {
            "@ag-grid-community/core": "~28.2.1"
        }
    },

这就有问题了。side-bar 依赖28.2.1,而我们安装的是28.2.0, 运行时找不到对应的版本所以报错了。解决方案:在package.json里强指一下:

1
2
3
4
    "resolutions": {
        "@ag-grid-community/core": "28.2.0",
        "@ag-grid-enterprise/core": "28.2.0"
    }

删掉ag-grid有关的module,删掉package-lock.json,重新install一下,之后再打开package-lock.json检查一下没有28.2.1这个版本。
demo code:github