json - 当我收到 '无法解析 "fs" 或 "child_process" ' 错误时,如何让我的 VS Code 插件构建?
初始化 SimpleGit 对象的这一行 const git: SimpleGit=simpleGit().clean(CleanOptions.FORCE); 导致我的项目构建出现问题,出现错误“无法解析 /node_modules/@kwsites/file-exists/dist/src 中的 'fs'”……“字段 'browser' 不包含有效的别名配置”(然后同样的错误会显示 'child_process' 代替 'fs')。
我正在使用这里“用法”下列出的最后一种方法导入 simple-git,并且也尝试了另外两种方法,所有方法都导致了这个错误。
// Option 3
import { simpleGit, SimpleGit, CleanOptions } from 'simple-git';
const git: SimpleGit = simpleGit().clean(CleanOptions.FORCE);
我最初按照这个指南来设置使用 esbuild 的 VS Code 插件。在花了一天时间试图弄清楚之后,我切换到了使用 WebPack 的这个版本(因为网上似乎有更多关于 WebPack 的帮助),但仍然无法找到可行的解决方案。
我构建时当前的错误是:
ERROR in ./node_modules/@kwsites/file-exists/dist/src/index.js 6:13-26
Module not found: Error: Can't resolve 'fs' in '/filepath/hello-world/node_modules/@kwsites/file-exists/dist/src'
resolve 'fs' in '/filepath/hello-world/node_modules/@kwsites/file-exists/dist/src'
Parsed request is a module
using description file: /filepath/hello-world/node_modules/@kwsites/file-exists/package.json (relative path: ./dist/src)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
/filepath/hello-world/node_modules/@kwsites/file-exists/dist/src/node_modules doesn't exist or is not a directory
/filepath/hello-world/node_modules/@kwsites/file-exists/dist/node_modules doesn't exist or is not a directory
/filepath/hello-world/node_modules/@kwsites/file-exists/node_modules doesn't exist or is not a directory
/filepath/hello-world/node_modules/@kwsites/node_modules doesn't exist or is not a directory
/filepath/hello-world/node_modules/node_modules doesn't exist or is not a directory
looking for modules in /filepath/hello-world/node_modules
single file module
using description file: /filepath/hello-world/package.json (relative path: ./node_modules/fs)
no extension
Field 'browser' doesn't contain a valid alias configuration
/filepath/hello-world/node_modules/fs doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
/filepath/hello-world/node_modules/fs.ts doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
/filepath/hello-world/node_modules/fs.js doesn't exist
@ ./node_modules/@kwsites/file-exists/dist/index.js 6:9-25
@ ./node_modules/simple-git/dist/cjs/index.js 261:25-56
@ ./src/webview/main.ts 14:21-42
webpack 5.75.0 compiled with 2 errors in 21770 ms
[webpack-cli] Compiler is watching files for updates...
预期的结果是一次成功的构建。
这是我尝试过的解决方案(但没有用):未找到模块:错误:无法解析 'fs',找不到模块 'fs'
Electron:模块未找到:错误:无法解析 'fs'。在使用 TypeScript、SystemJS 和 Electron 的情况下,需要 Node.js 的 "child_process"。这是一个我没有尝试过的解决方案(因为我不理解所说内容/信息不够):https://github.com/NativeScript/nativescript-dev-appium/issues/223#issuecomment-498569080
以及我查过但没有找到对我的应用程序有好答案的地方列表:NodeJS - 在 child_process 中找不到模块 找不到名称 'module'
在阅读了《如何修复找不到模块 'fs'》和《未捕获错误:找不到模块 "fs"》之后,似乎我的客户端和服务器端开发理解之间可能存在问题(我不完全确定是否是我在 WebView 应用中使用 simple-git 导致了这些问题)。
如果有人有任何见解,或者我还没有尝试过的额外评论或解决方案,我将非常感激你的帮助。谢谢!
编辑:我也在尝试修改文件,并且将文件系统作为变量导入的方法也会导致相同的错误信息:var fs=require('fs');