我有一个javascript文件以 require()
开头,它在 vscode 中工作正常按 F5,它会在调试控制台中返回输出:
require('../src/FoxRandomString.js');
obj = FoxRandomString;
Tests = {
obj: obj,
canSetLength: function(){
vals = [true,false,-1,'text',undefined,null,0,2,4,10]
for (i = 0; i < vals.length; i++){
if (vals[i] < 4){
if (this.obj.setLength(vals[i]) == 4){
console.log('%cFor input value = '+vals[i]+ ' Success. Output: '+this.obj.setLength(vals[i]),'color: white; background:olive')
}
else{
console.log('%cFor input value = '+vals[i]+ ' Fail. Output: '+this.obj.setLength(vals[i]),'color:black; background:red')
}
}
else{
if (this.obj.setLength(vals[i]) == vals[i] || isNaN(vals[i])){
console.log('%cFor input value = '+vals[i]+ ' Success. Output: '+this.obj.setLength(vals[i]),'color: white; background:olive')
}
else{
console.log('%cFor input value = '+vals[i]+ ' Fail. Output: '+this.obj.setLength(vals[i]),'color:black; background:red')
}
}
}
}
}
Tests.canSetLength();
但是,使用脚本标记将此脚本包含在 HTML 文件中并在 Google Chrome 等浏览器中打开它,会生成有关 require() is not Defined
的控制台错误。我需要了解为什么会发生这种情况?
最佳答案
这是因为浏览器(客户端 JavaScript)中不存在 require()。 您可以使用以下选项:
<script> tag.
CommonJS implementation, like Node.js
one more requireJS also can do the job done.
关于javascript - VS Code 调试控制台与任何 Web 浏览器有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52688335/