Javascript Electron:导入我自己的.js文件时出现问题
我正在发现Electron,我面临一个问题:我无法在我的“test.js”脚本中实现我的“common.js”文件 以下是我的项目的体系结构:Javascript Electron:导入我自己的.js文件时出现问题,javascript,node.js,module,electron,Javascript,Node.js,Module,Electron,我正在发现Electron,我面临一个问题:我无法在我的“test.js”脚本中实现我的“common.js”文件 以下是我的项目的体系结构: rootProject -features(folder) -common.js -test.js -index.html common.js const hello = "hello"; module.exports = { hello }; const {hello} = require(&
rootProject
-features(folder)
-common.js
-test.js
-index.html
common.js
const hello = "hello";
module.exports = { hello };
const {hello} = require("./common.js");
console.log(hello);
<script src="./features/common.js"></script>
test.js
const hello = "hello";
module.exports = { hello };
const {hello} = require("./common.js");
console.log(hello);
<script src="./features/common.js"></script>
index.html
<body>
<h1>Hello World!</h1>
<div class="flex-container">
<span id="timeAfterRefreshGasFees"></span>
<span id="rapid"></span>
<span id="fast"></span>
<span id="standard"></span>
<span id="slow"></span>
</div>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
<!-- <script src="./features/common.js"></script> -->
<script src="./features/test.js"></script>
</body>
并删除了问题在于客户端JavaScript中不存在require()
更成熟的答案是:问题在于客户端JavaScript中不存在require()
更详细的回答是:像@brunocordioli072所说的
require
只是一个Node.js函数,默认情况下,Electron中的Node.js函数是禁用的
因为这是Javascript,所以可以像包含文件一样使用
标记
如果我像@brunocordioli072一样取消注释,则表示
require
只是一个Node.js函数,默认情况下,Electron中禁用了Node.js函数
因为这是Javascript,所以可以像包含文件一样使用
标记
如果我取消注释,好的!谢谢你的回答。我知道如果我使用集成,我可以在没有类的情况下调用函数/var。但我需要使用“require”来实现npm包。因此,在“nodeIntegration:true”中,我得到了一个错误:“找不到模块”。/common.js'”。检查原始位置如果您像以前一样打开了
nodeIntegration
,您可以使用require
导入NPM模块,但不能导入文件,因此require('./common.js')
仍然无法工作。因此,重新添加
标记,现在nodeIntegration
已打开,您可以执行如下操作:require('fs')
Ok!谢谢你的回答。我知道如果我使用集成,我可以在没有类的情况下调用函数/var。但我需要使用“require”来实现npm包。因此,在“nodeIntegration:true”中,我得到了一个错误:“找不到模块”。/common.js'”。检查原始位置如果您像以前一样打开了nodeIntegration
,您可以使用require
导入NPM模块,但不能导入文件,因此require('./common.js')
仍然无法工作。因此,重新添加
标记,现在nodeIntegration
已打开,您可以执行以下操作:require('fs')