Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在浏览器端运行node.js后端脚本时,如何定义_dirname?_Javascript_Node.js - Fatal编程技术网

Javascript 在浏览器端运行node.js后端脚本时,如何定义_dirname?

Javascript 在浏览器端运行node.js后端脚本时,如何定义_dirname?,javascript,node.js,Javascript,Node.js,在浏览器端运行脚本时出现错误。错误为“未捕获引用错误:\未定义目录名”。我尝试使用const_uudirname=dirname(fileURLToPath(URL));在下面的查询中,但它不起作用。有人能帮忙解决这个问题吗 var fs= require(['fs']); var path= require(['path']); dName=path.dirname; URL=path.fileURLToPath; //import { fileURLToPath } from 'url';

在浏览器端运行脚本时出现错误。错误为“未捕获引用错误:\未定义目录名”。我尝试使用const_uudirname=dirname(fileURLToPath(URL));在下面的查询中,但它不起作用。有人能帮忙解决这个问题吗

var fs= require(['fs']);
var path= require(['path']);
dName=path.dirname;
URL=path.fileURLToPath;
//import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(URL));

var fname= "config.json"
var jPath= path.join(__dirname,"..","ConfigGen","Config",fname);
var jsString= fs.readFileSync(jPath, 'utf8')

var tType= "cto"                        //Get this from input
var pth= JSON.parse(jsString)[tType]    //perform error handling

var cType = "jbod"                      //Get this from input
//Use that path 
fs.readdir(pth, function(err,files) {
    files.forEach(function(file){
        fName= cType+"_"+tType+"_uut.yaml-example";
        if(file==fName){
            var flContent= fs.readFileSync(path.join(pth,file),"utf8")
            console.log(flContent)
        }
    })
})

注意:它在节点中运行良好。在浏览器中抛出错误(我正在Chrome中运行)

为什么希望此代码在浏览器中运行?我的意思是,这是一个Node.js代码——要在服务器上运行,它与浏览器不兼容。我们需要更多关于你的案例的细节,比如你想实现什么,到目前为止你做了什么。(可能您使用类似browserify的工具或其他什么?)没有更多详细信息,我可以告诉您,
\uu dirname
是一个Node.js变量,其值是当前模块的目录名。它仅在Node.js环境中可用,不能在浏览器中使用(除非您自己定义
\uu dirname
,但它不是同一个变量)