fs.writeFile 不适用于我的 ReactJs/NodeJs 应用程序 [英] fs.writeFile is not working on my ReactJs / NodeJs app

查看:153
本文介绍了fs.writeFile 不适用于我的 ReactJs/NodeJs 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 reactJS 和 nodeJs 在文件中写一些东西,但它不起作用.这是我的代码,是一个简单的代码,我知道 fs.writeFile 不是一个函数.对于我从 fs 使用的每个函数,我都会收到此错误.在 node_module 中,我有 fs 的文件夹,但在里面我只有 package.json 和自述文件.我不知道该怎么办,我在网上搜索了答案,但一无所获.任何帮助都会很棒

import React, { Component } from 'react'const fs = require('fs');导出默认类 WriteFile 扩展组件 {写 = () =>{fs.writeFile("file.txt", "你好!", function(err) {如果(错误){返回 console.log(err);}console.log("文件已保存!");});}使成为() {返回 (<div><button className='btn btn-info' onClick={this.write}>scria-ti-as</button>

)}}

和错误

TypeError: fs.writeFile 不是函数WriteFile._this.writeD:/js/login_reactjs/src/components/WriteFile.js:85 |导出默认类 WriteFile 扩展组件 {6 |7 |写 = () =>{8 |fs.writeFile("file.txt", "你好!", function(err) {9 |如果(错误){10 |返回 console.log(err);11 |}

解决方案

fs 在浏览器中不起作用.这是设计使然.fs 已被淘汰见这张票

如果使用支持 browserifybrowserFS

I am trying to write some stuff in a file using reactJS and nodeJs but it doesnt work. Here is my code, is a simple one and i get that fs.writeFile is not a function. I get this error for each function that i use from fs. In node_module i have the folder for fs but inside i only have package.json and a readme. I dont know what to do, i searched online for answers but nothing. Any help would be awesome

import React, { Component } from 'react'

const fs = require('fs');

export default class WriteFile extends Component {

write = () => {
    fs.writeFile("file.txt", "Hey there!", function(err) {
        if(err) {
            return console.log(err);
        }

        console.log("The file was saved!");
    }); 
}

render() {
    return (
    <div>
        <button className='btn btn-info' onClick={this.write}>scria-ti-as</button>
    </div>
    )
 }
}

and the error

TypeError: fs.writeFile is not a function

WriteFile._this.write

D:/js/login_reactjs/src/components/WriteFile.js:8

5 | export default class WriteFile extends Component {

6 | 

7 |     write = () => {

8 |         fs.writeFile("file.txt", "Hey there!", function(err) {

9 |             if(err) {

10 |                 return console.log(err);

11 |             }

解决方案

fs will not work in the browser. This is by design. fs has been stubbed out See this ticket

You would have better luck using packages that support browsers like browserify or browserFS

这篇关于fs.writeFile 不适用于我的 ReactJs/NodeJs 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆