我在做项目时,使用了common.js作为Module,在main.js中按需引用common.js的函数,现在发现,如果用手机的autojs6直接运行保存在手机中的项目文件,比如main.js是可以正常运行并调用common.js的函数。但如果使用vscode运行的时候,则报错,提示:
`
13:51:47.771/V: 开始运行 [$remote/main.js].
13:51:47.795/E: Error: Can't resolve relative module ID "./common/common.js" when require() is used outside of a module (file:///android_asset/modules/jvm-npm.js#100)
Error: Can't resolve relative module ID "./common/common.js" when require() is used outside of a module
at file:///android_asset/modules/jvm-npm.js:100:0
at file:///android_asset/modules/jvm-npm.js:64:0
at $remote/main.js:2:0
13:51:47.796/V: [$remote/main.js] 运行结束 (用时 0.024 秒)
`
我查了很多资料未查到原因,只看到一些说法:比如
1.将文件放到手机里才可以正常(的确,放手机里后,是可以运行,但只能通过手机里的autojs运行是正常的,但是我在电脑的vscode调试和写代码后的运行仍然不能运行)。
2.需要用vscode的autojs插件中的Autojs6: 运行项目(run project)(快捷键是Alt + F6)。这个的确可以运行了。但当我通过点击工具按钮的方式运行项目时,又提示另一个错误,而且很奇怪的错误:“缺少必要的项目配置文件: d:\code-java\京东农场\main.js\project.json”,我是没办法创建这样的目录结构的,因为main.js是已经存在的文件而非文件夹。)
请问,这应该怎样解决以上问题呢?
附:
我的common.js文件为:(且我确定这个模块的功能是正常的,因为在手机内的autojs6中是可以正常运行的,只是引用到这里时,没有了缩进)
`
var common= {};
common.sum=function(a,b){
return a+b;
};
common.getScreenCapture=function(){
let Thread = threads.start(function(){
if(auto.service != null){ //如果已经获得无障碍权限
//由于系统间同意授权的文本不同,采用正则表达式
let Allow = textMatch(/^允许$|^同意$|^立即开始$/).findOne(10*1000);
if(Allow){
Allow.click();
}
}
});
if(!requestScreenCapture()){
log("请求截图权限失败");
return false;
}else{
Thread.interrupt()
log("已获得截图权限");
return true;
}
};
module.exports = common;
我的main.js文件为:(且我确定这个代码的功能是正常的,因为在手机内的autojs6中是可以正常运行的)
auto();
var common = require("./common/common.js");
common.getScreenCapture();
`
我在做项目时,使用了common.js作为Module,在main.js中按需引用common.js的函数,现在发现,如果用手机的autojs6直接运行保存在手机中的项目文件,比如main.js是可以正常运行并调用common.js的函数。但如果使用vscode运行的时候,则报错,提示:
`
13:51:47.771/V: 开始运行 [$remote/main.js].
13:51:47.795/E: Error: Can't resolve relative module ID "./common/common.js" when require() is used outside of a module (file:///android_asset/modules/jvm-npm.js#100)
Error: Can't resolve relative module ID "./common/common.js" when require() is used outside of a module
at file:///android_asset/modules/jvm-npm.js:100:0
at file:///android_asset/modules/jvm-npm.js:64:0
at $remote/main.js:2:0
13:51:47.796/V: [$remote/main.js] 运行结束 (用时 0.024 秒)
`
我查了很多资料未查到原因,只看到一些说法:比如
1.将文件放到手机里才可以正常(的确,放手机里后,是可以运行,但只能通过手机里的autojs运行是正常的,但是我在电脑的vscode调试和写代码后的运行仍然不能运行)。
2.需要用vscode的autojs插件中的Autojs6: 运行项目(run project)(快捷键是Alt + F6)。这个的确可以运行了。但当我通过点击工具按钮的方式运行项目时,又提示另一个错误,而且很奇怪的错误:“缺少必要的项目配置文件: d:\code-java\京东农场\main.js\project.json”,我是没办法创建这样的目录结构的,因为main.js是已经存在的文件而非文件夹。)
请问,这应该怎样解决以上问题呢?
附:
我的common.js文件为:(且我确定这个模块的功能是正常的,因为在手机内的autojs6中是可以正常运行的,只是引用到这里时,没有了缩进)
`
var common= {};
common.sum=function(a,b){
return a+b;
};
common.getScreenCapture=function(){
let Thread = threads.start(function(){
if(auto.service != null){ //如果已经获得无障碍权限
//由于系统间同意授权的文本不同,采用正则表达式
let Allow = textMatch(/^允许$|^同意$|^立即开始$/).findOne(10*1000);
if(Allow){
Allow.click();
}
}
});
if(!requestScreenCapture()){
log("请求截图权限失败");
return false;
}else{
Thread.interrupt()
log("已获得截图权限");
return true;
}
};
module.exports = common;
我的main.js文件为:(且我确定这个代码的功能是正常的,因为在手机内的autojs6中是可以正常运行的)auto();
var common = require("./common/common.js");
common.getScreenCapture();
`