Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

作为pnpm workspace 的依赖项使用时无法找到模块'@easyops-cn/docusaurus-search-local/dist/client/shared/lunrLanguageZh' #459

Closed
liang-tian-yu opened this issue Oct 2, 2024 · 0 comments · Fixed by #461 or #462

Comments

@liang-tian-yu
Copy link

我的项目结构是 将@easyops-cn/docusaurus-search-local作为pnpm workspace packages/preset 的一个依赖,然后将package/preset 通过workspace协议链接到项目根目录中使用

根目录是用create-docusaurus 创建的一个网站

在根目录运行docusaurus start报错如下:

Module not found: Error: Can't resolve '@easyops-cn/docusaurus-search-local/dist/client/shared/lunrLanguageZh' in 'C:\Users\admin\Documents\Projects\website.docusaurus@easyops-cn\docusaurus-search-local\default'

检查后发现

.docusaurus\@easyops-cn\docusaurus-search-local\default\generated.js 中的内容

import lunr from "C:\\Users\\admin\\Documents\\Projects\\website\\node_modules\\.pnpm\\[email protected]\\node_modules\\lunr\\lunr.js";
require("C:\\Users\\admin\\Documents\\Projects\\website\\node_modules\\.pnpm\\[email protected]\\node_modules\\lunr-languages\\lunr.stemmer.support.js")(lunr);
require("@easyops-cn/docusaurus-search-local/dist/client/shared/lunrLanguageZh").lunrLanguageZh(lunr);
require("C:\\Users\\admin\\Documents\\Projects\\website\\node_modules\\.pnpm\\[email protected]\\node_modules\\lunr-languages\\lunr.multi.js")(lunr);

报错原因在第三行,因为pnpm特殊的node_modules结构,这个模块在主项目中无法直接引入
第二行没报错,可以考虑将导入方式也统一换成绝对链接

对应的源代码在docusaurus-search-local/src/server/utils/generate.ts 第53行,改成

    contents.push(
      `require(${JSON.stringify(
        require.resolve("@easyops-cn/docusaurus-search-local/dist/client/shared/lunrLanguageZh")
      )}).lunrLanguageZh(lunr);`
    );

编译了一下没发现问题,但是暂时没法在我的网站上测试,能否帮忙修复一下 ,谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant