我正在尝试使用Typescript进行电子开发.在为节点和jquery打字之后,我终于得到了我的.ts文件错误.
问题是,当我运行我的应用程序时,我收到此错误:
index.js:2 Uncaught ReferenceError: exports is not defined
Run Code Online (Sandbox Code Playgroud)
这些是index.js中的前两行:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Run Code Online (Sandbox Code Playgroud)
我不知道那条线.打字稿在编译时添加了它.如果删除它,我的应用程序工作正常.
我该如何摆脱这个错误?
哦,这是我的tsconfig,如果这是相关的.
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"isolatedModules": false,
"jsx": "react",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": false,
"noImplicitAny": false,
"noImplicitUseStrict": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true
},
"exclude": [
"node_modules",
"typings/browser",
"typings/browser.d.ts"
],
"compileOnSave": true,
"buildOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
Run Code Online (Sandbox Code Playgroud) 根据文档 node.js,fs.exists()将被弃用.他们的推理:
fs.exists()是一种时代错误,只是出于历史原因而存在.几乎没有理由在你自己的代码中使用它.
特别是,在打开文件之前检查文件是否存在是一种反模式,使您容易受到竞争条件的影响:另一个进程可能会在调用fs.exists()和fs.open()之间删除该文件.只需打开文件并在错误处理时处理错误.
fs.exists()将被弃用.
我目前在移动文件之前使用它,因为fs.rename()似乎在目标文件夹中安静地覆盖具有相同名称的文件.
我的问题是; 我应该使用什么来防止fs.rename()覆盖目标文件夹中的文件?我认为有一种我不知道的方式.否则我没有看到fs.exists()被弃用的原因.
由于我不想打开文件,因此建议使用fs.open()似乎有些过分.
编辑,按照@jfriend00的要求获取更多关于我正在做什么的信息.
我正在制作一个Electron应用程序,用户可以将文件分类到不同的目录中.它不是服务器软件,它旨在每天运行用户机器,处理他们的文档.这是到目前为止移动文件的代码:
function moveFile(destIndex){
var from = queue[currentQueueIndex].path;
var to = destinations[destIndex].path + path.sep + path.basename(from);
console.log("[i] Move file (from/to): ");
console.log(from);
console.log(to);
//Check if file exists, if yes: give them the choice to cancel.
fs.stat(to, function (err, stats) {
if (err){
move(from, to);
} else {
var confirmed = confirm("File already exists, will overwrite.");
if (confirmed) {
move(from, to);
}
}
});
next(); //Show the next …Run Code Online (Sandbox Code Playgroud)