构建和运行 vscode 源代码时无法成功 require('electron')

Posted

技术标签:

【中文标题】构建和运行 vscode 源代码时无法成功 require(\'electron\')【英文标题】:Can't require('electron') successfully when build and run vscode source code构建和运行 vscode 源代码时无法成功 require('electron') 【发布时间】:2022-01-06 11:54:00 【问题描述】:

我正在从源代码运行 vscode 并按照以下步骤操作:

    下载vscode源码(版本:1.63.0)
 "name": "code-oss-dev",
 "version": "1.63.0",
 "distro": "1aa3ab55b3cceca22ca6d647dc0095d562d23c8d",
    运行yarn 安装软件包。 运行yarn watch 构建并成功完成。
......
[watch-extensions] [23:54:02] Finished webpacking extension media markdown-language-features with 0 errors.
[watch-extensions] [23:54:02] Finished webpacking extension media simple-browser with 0 errors.
[watch-extensions] [23:54:14] Finished compilation extensions with 0 errors after 44266 ms
[watch-client    ] [23:55:24] Finished compilation with 0 errors after 111430 ms
    运行./scripts/code.sh 启动但失败。
localhost:vscode-main$ ./scripts/code.sh 
yarn run v1.22.10
$ node build/lib/electron
✨  Done in 3.27s.
[00:00:37] Syncronizing built-in extensions...
[00:00:37] You can manage built-in extensions with the --builtin flag
[00:00:38] Downloading extension: ms-vscode.references-view@0.0.81 ...
[00:00:38] Downloading extension: ms-vscode.js-debug-companion@1.0.15 ...
[00:00:38] Downloading extension: ms-vscode.js-debug@1.62.0 ...
[00:00:38] Downloading extension: ms-vscode.vscode-js-profile-table@0.0.18 ...
[00:00:38] [marketplace] ms-vscode.vscode-js-profile-table ✔︎
[00:00:38] [marketplace] ms-vscode.references-view ✔︎
[00:00:38] [marketplace] ms-vscode.js-debug-companion ✔︎
[00:00:39] [marketplace] ms-vscode.js-debug ✔︎
/Users/xxx/vscode-main/out/main.js:28
app.allowRendererProcessReuse = false;
                              ^

TypeError: Cannot set property 'allowRendererProcessReuse' of undefined
    at Object.<anonymous> (/Users/xxx/vscode-main/out/main.js:28:31)
    at Module._compile (internal/modules/cjs/loader.js:1078:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1108:10)
    at Module.load (internal/modules/cjs/loader.js:935:32)
    at Module._load (internal/modules/cjs/loader.js:776:14)
    at Function.f._load (electron/js2c/asar_bundle.js:5:12913)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

main.js 中的相对代码是

const  app, protocol, crashReporter  = require('electron');

app.allowRendererProcessReuse = false;

我添加了一个日志

......
const  app, protocol, crashReporter  = require('electron');
const abc = require('electron'); //added
console.log(abc); //added
console.log('isString?', typeof abc === 'string'); //added
app.allowRendererProcessReuse = false;

结果是

localhost:vscode-main$ ./scripts/code.sh 
yarn run v1.22.10
$ node build/lib/electron
✨  Done in 2.07s.
[00:11:51] Syncronizing built-in extensions...
[00:11:51] You can manage built-in extensions with the --builtin flag
[00:11:51] [marketplace] ms-vscode.references-view@0.0.81 ✔︎
[00:11:51] [marketplace] ms-vscode.js-debug-companion@1.0.15 ✔︎
[00:11:51] [marketplace] ms-vscode.js-debug@1.62.0 ✔︎
[00:11:51] [marketplace] ms-vscode.vscode-js-profile-table@0.0.18 ✔︎
/Users/xxx/vscode-main/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron
isString? true
/Users/xxx/vscode-main/out/main.js:30
app.allowRendererProcessReuse = false;
                              ^

TypeError: Cannot set property 'allowRendererProcessReuse' of undefined
    at Object.<anonymous> (/Users/xxx/vscode-main/out/main.js:30:31)

为什么这里需要将require('electron')解析成字符串?

【问题讨论】:

【参考方案1】:

问题已解决。

执行命令env后, 我发现有一个环境变量ELECTRON_RUN_AS_NODE=1。 (我从来没有设置过这个变量...)

执行unset ELECTRON_RUN_AS_NODE,确认变量不存在。

require('electron') 现在可以工作了。

【讨论】:

以上是关于构建和运行 vscode 源代码时无法成功 require('electron')的主要内容,如果未能解决你的问题,请参考以下文章

从源代码构建 VSCode 后安装扩展

Flutter - FAILURE:构建失败并出现异常。 - 无法启动守护进程。 - 安卓工作室 - VSCode

网络问题导致vscode无法运行测试用例

VSCode语法错误突出显示不适用于C ++代码

无法获得离子来构建android apk

在Anaconda基本环境VScode中无法导入库错误