Node.js,Webpack
在这个使用webpack的项目中,安装了FS。此代码需要读取文件,但返回错误“未捕获的TypeError:fs.readFile不是函数”
const bookForm = document.querySelector(".book-form");
const select = document.querySelector(".select"); const fs = require("fs");
export function abc() { bookForm.addEventListener("submit", e => {
console.log(select.options[select.selectedIndex].text);
e.preventDefault();
fs.readFile("file.txt", function(error, data) {
console.log("file read");
if (error) throw error;
console.log(data); });
}); }
您无法fs
在浏览器中导入模块,因为浏览器环境无法访问用户的文件系统。fs
仅在Node.js上下文(在服务器上)可用,而在客户端(浏览器)上不可用。
如果要将文件从浏览器发送到服务器,可以使用<input type="file">
并让用户手动选择他们必须发送的文件。如果要将服务器文件的内容发送到浏览器,则可以使用HTTP通信(AJAX),也可以在服务器端计算的HTML模板中呈现它的内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句