我有过
import fs from 'fs'
在我的package.json里我有
然后我运行该命令
> npm i fs
> fs@0.0.2 node_modules/fs
接下来,在我的React store中,我导入了'fs‘模块
import fs from 'fs'
但是,当我尝试使用fs时
除了构造器和其他一些__methods之外,我看不到任何方法。我没有看到createReadStream方法或任何其他文件操作方法。
有人知道哪里出了问题吗?(使用Webpack),并可以根据要求提供更多信息,但我已经走到这里了……
ps:为什么我可以npm i fs --当我在其他帖子上读到我不需要这样做时,我可以保存(使用节点5.5.0)
import Reflux from 'reflux'
import AddItemActions from '../actions/AddItemActions'
import request from 'superagent-bluebird-promise'
import fs from 'fs'
var ImageStore = Reflux.createStore({
init(){
.
.
.
},
decryptImage(file) {
var reader = new FileReader();
var info = {}
reader.onload = (output) => {
debugger
request.post("https://camfind.p.mashape.com/image_requests")
.set("X-Mashape-Key", "KEY")
.set("Content-Type", "application/x-www-form-urlencoded")
.set("Accept", "application/json")
.send({'focus': { 'x': 480}})
.send({'focus': { 'y': 640}})
.send({'image_request': {'altitude': 27.912109375}})
.send({'image_request': {'language': "en"}})
.send({'image_request': {'latitude': 35.8714220766008}})
.send({'image_request': {'locale' : "en_US"}})
.send({'image_request': {'longitude': 14.3583203002251}})
.send({'image_request': {'image': fs.createReadStream("/path" + 'file.jpg')}})
.then(function (result) {
console.log(result.status, result.headers, result.body);
this.info = result
},
function(error) {
console.log(error);
})
}
reader.readAsDataURL(file);
return info
},
.
.
.
.
})
发布于 2018-02-19 04:11:28
在create-react-app
中,他们去掉了'fs‘。您不能导入它。他们这样做是因为fs
是一个节点核心模块。
你必须找到另一个解决方案来解决这个问题。请参阅this ticket.
发布于 2018-01-21 03:44:35
这可能是一个环境问题。浏览器无法解释和运行某些Node服务器端模块,如fs
。
解决方案是在节点环境(服务器端)中运行fs
方法,或者查找提供相同功能但为浏览器编写的包。
这个问题已经讨论过了.Module not found: Error: Cannot resolve module 'fs'
这个问题..。Use fs module in React.js,node.js, webpack, babel,express
发布于 2022-01-07 08:09:37
npm i babel-plugin-preval
您可以在React中使用 ,如果您想在React中使用节点的fs,如下所示
const greetingContent = preval`
const fs = require('fs')
module.exports = fs.readFileSync(require.resolve('./greeting.txt'), 'utf8')
`
console.log(greetingContent);
https://stackoverflow.com/questions/35003961
复制相似问题