使用node.js方法时出错path.extname()

我正在使用电子。 我写了一个recursion读取目录文件的程序。 现在我想要一个特定的extname(文件扩展名)文件不显示。 为了做到这一点,我需要node.js方法path.extname(path),它返回extname。 我的问题是path.extname()方法不能在我需要它的代码的地方工作。 除了我的函数scan_directory_to_html()它的工作原理和返回.jpg但函数内部我得到错误Uncaught TypeError: path.extname is not a function

 const fs = require('fs'); const path = require('path'); const {ipcRenderer} = require('electron') //This works: console.log(path.extname(`${__dirname}/../../project_files/sprites/Appenzell.jpg`)); function scan_directory_to_html(directory){ var zw_directory_array = fs.readdirSync(directory); var zw_to_html = ""; for(var i = 0; i < zw_directory_array.length; i++){ var path = directory + zw_directory_array[i]; //This produces the error message console.log(path.extname(`${__dirname}/../../project_files/sprites/Appenzell.jpg`)); if(fs.lstatSync(path).isFile()){ zw_to_html += "<a href='#' onClick='create_sprite_window(`"+ path +"`)'><li><img src='" + path + "'/>" + zw_directory_array[i] + "</li></a>"; }else if(fs.lstatSync(path).isDirectory()){ zw_to_html += "<li class='li_directory'>" + zw_directory_array[i] + "</li>"; zw_to_html += "<ul>"; zw_to_html += scan_directory_to_html(path + "/"); zw_to_html += "</ul>"; }else{ console.log("Error in function scan_directory_to_html(): Path is neither directory nor file."); } } return zw_to_html; } document.getElementById('sidebar_left_sprites').innerHTML = scan_directory_to_html(`${__dirname}/../../project_files/sprites/`); 

我也试图把const path = require('path'); 在函数内部,但是它表示标识符'path'已经被声明。 如何在函数内部使用path.extname()方法?

path在您的代码中发生冲突。 在下面使用另一个名字。

 var filepath = directory + zw_directory_array[i]; 

代替

 var path = directory + zw_directory_array[i];