jprichardson / node-jsonfile

Easily read/write JSON files.
MIT License
1.2k stars 321 forks source link

TypeError: fs.readFile is not a function #97

Closed iiidefektiii closed 6 years ago

iiidefektiii commented 6 years ago

I am using your example to read data and getting an error with the following Error on line 26 of index.js 26 | fs.readFile(file, options, function (err, data) {

using the following example:

var jsonfile = require('jsonfile')
var file = '/tmp/data.json'
jsonfile.readFile(file, function(err, obj) {
  console.dir(obj)
})
RyanZim commented 6 years ago

What's your OS? Try removing node_modules and running npm install again.

iiidefektiii commented 6 years ago

OS is Windows 10 I will try removing it and check.

iiidefektiii commented 6 years ago

Instead of deleting the node_modules folder I created a new React project and installed the jsonfile module only and still get the same error on the same line when trying to use readFile. But even if I try to use fs.readFile bypassing the jsonfile I still get the error.

var fs= require('fs') fs.readFile('myjsonfile.json')

Gets me the same error. Am I missing some kind of dependency?

Marak commented 6 years ago

What version of node and npm are you running?

iiidefektiii commented 6 years ago

Node Version: v6.11.4 npm Version: 5.5.1

RyanZim commented 6 years ago

Something is seriously messed up on your computer; try reinstalling Node & npm

iiidefektiii commented 6 years ago

I don't have an issue running anything else tho.

iiidefektiii commented 6 years ago

I am going to test on a second PC later. With everything freshly installed. I will let you know if I get the same issue there.

iiidefektiii commented 6 years ago

I have installed node and npm on a new machine that I wasn't using before. Only thing in common is Windows 10. Still the same error at the same line when trying to read or write.

johndobrien commented 6 years ago

Works for me.

C:\Users\john\jsonfileTest>ver

Microsoft Windows [Version 10.0.17025.1000]

C:\Users\john\jsonfileTest>node --version
v6.11.4

C:\Users\john\jsonfileTest>type test.js
var jsonfile = require('jsonfile');
var file = 'data.json'
jsonfile.writeFile(file, '{"this": true}', function (err) {
        if (err) console.log(err);
});
jsonfile.readFile(file, function (err, obj) {
 console.log(obj);
});

C:\Users\john\jsonfileTest>node test.js
{"this": true}
Sarkani commented 6 years ago

Same problem here. Its not working always getting same message TypeError: fs.readFile is not a function

23 | shouldThrow = options.throws 24 | } 25 |

26 | fs.readFile(file, options, function (err, data) { 27 | if (err) return callback(err) 28 | 29 | data = stripBom(data)

RyanZim commented 6 years ago

OK, everyone here who is having this issue, please post your Node.js version and the output of npm ls graceful-fs, run from the project that's having this issue.

iiidefektiii commented 6 years ago

Node Version: v6.11.4 npm Version: 5.5.1

+-- fs-@1.0.0
| `-- yargs@5.0.0
|   `-- read-pkg-up@1.0.1
|     `-- read-pkg@1.1.0
|       +-- load-json-file@1.1.0
|       | `-- graceful-fs@4.1.11  deduped
|       `-- path-type@1.1.0
|         `-- graceful-fs@4.1.11  deduped
+-- graceful-fs@4.1.11
+-- jsonfile@4.0.0
| `-- graceful-fs@4.1.11  deduped
+-- react-native@0.50.3
| +-- fs-extra@1.0.0
| | +-- graceful-fs@4.1.11  deduped
| | +-- jsonfile@2.4.0
| | | `-- graceful-fs@4.1.11  deduped
| | `-- klaw@1.3.1
| |   `-- graceful-fs@4.1.11  deduped
| +-- graceful-fs@4.1.11  deduped
| +-- metro-bundler@0.20.3
| | +-- graceful-fs@4.1.11  deduped
| | +-- jest-haste-map@21.2.0
| | | `-- graceful-fs@4.1.11  deduped
| | `-- write-file-atomic@1.3.4
| |   `-- graceful-fs@4.1.11  deduped
| `-- yargs@9.0.1
|   `-- read-pkg-up@2.0.0
|     `-- read-pkg@2.0.0
|       `-- load-json-file@2.0.0
|         `-- graceful-fs@4.1.11  deduped
`-- react-scripts@1.0.17
  +-- eslint@4.10.0
  | `-- file-entry-cache@2.0.0
  |   `-- flat-cache@1.3.0
  |     `-- graceful-fs@4.1.11  deduped
  +-- eslint-plugin-import@2.8.0
  | `-- read-pkg-up@2.0.0
  |   `-- read-pkg@2.0.0
  |     `-- load-json-file@2.0.0
  |       `-- graceful-fs@4.1.11  deduped
  +-- fs-extra@3.0.1
  | +-- graceful-fs@4.1.11  deduped
  | `-- jsonfile@3.0.1
  |   `-- graceful-fs@4.1.11  deduped
  +-- jest@20.0.4
  | `-- jest-cli@20.0.4
  |   +-- graceful-fs@4.1.11  deduped
  |   +-- jest-haste-map@20.0.5
  |   | `-- graceful-fs@4.1.11  deduped
  |   +-- jest-jasmine2@20.0.4
  |   | `-- graceful-fs@4.1.11  deduped
  |   +-- jest-runtime@20.0.4
  |   | `-- graceful-fs@4.1.11  deduped
  |   `-- jest-util@20.0.3
  |     `-- graceful-fs@4.1.11  deduped
  +-- sw-precache-webpack-plugin@0.11.4
  | `-- sw-precache@5.2.0
  |   `-- update-notifier@1.0.3
  |     `-- configstore@2.1.0
  |       `-- graceful-fs@4.1.11  deduped
  +-- webpack@3.8.1
  | +-- enhanced-resolve@3.4.1
  | | `-- graceful-fs@4.1.11  deduped
  | +-- watchpack@1.4.0
  | | `-- graceful-fs@4.1.11  deduped
  | `-- yargs@8.0.2
  |   `-- read-pkg-up@2.0.0
  |     `-- read-pkg@2.0.0
  |       `-- load-json-file@2.0.0
  |         `-- graceful-fs@4.1.11  deduped
  +-- webpack-dev-server@2.9.4
  | `-- chokidar@1.7.0
  |   `-- readdirp@2.1.0
  |     `-- graceful-fs@4.1.11  deduped
  `-- webpack-manifest-plugin@1.3.2
    `-- fs-extra@0.30.0
      +-- graceful-fs@4.1.11  deduped
      `-- jsonfile@2.4.0
        `-- graceful-fs@4.1.11  deduped
Sarkani commented 6 years ago

Everything is working, it was my stupid mistake. I still learning and I done stupid mistake. I was trying to update my json file inside react app, but actually you need to update your json file from server and when do request from react app to server. iiidefektiii you putting that code in server side or your app?

iiidefektiii commented 6 years ago

I thought it was App? I'm not sure what you mean when you say server side? I thought this was to be placed in the App to read the json file. What does your setup look like then?

Either way I found a set up to accomplish what I needed to do bypassing this package to read a json file.

And if it is server side only why is that key info not in the readme so everyone knows?

Sarkani commented 6 years ago

I don't know but I set up a API with node and express, and when in my app I requesting data from my API and its working fine my node: app.get('/ , (req, res)=>{ var file = myfile.json' jsonfile.readFile(file, (err, obj) => {    console.log("Done"); })

and in my app fetch('http://localhost:3001/, { method:"get", headers:{ 'Accept': 'application/json', 'Content-Type': 'application/json' } }) .then(response => {
return response.json() }) .then(data => { console.log(data);
});

I don't know this is the way or no but I could not update my json from my app only from node, this is why I created API, if you need just to read you can use same code in my app just link will be your json file folder and you don't need any api

iiidefektiii commented 6 years ago

Ok so I created a js file and put the following in it. var jsonfile = require('jsonfile') var file = './exampledata.json' jsonfile.readFile(file, function(err, obj) { console.dir(obj) })

when I run node myjsonreader.js I get the data { id: 1, name: 'A green door', price: 12.5, tags: [ 'home', 'green' ] }

but when I try and bring it in to the app with your fetch I get: SyntaxError: Unexpected token < in JSON at position 0 at line: return response.json()

I see how it is working from the js file but how do i get that into my react app now?

Edit: if i put the json file on my server I can get it, but how can I get it locally like you are to test? Or is that the only way to read if its on a public server somewhere?

Sarkani commented 6 years ago

I just tried and its working for me. Where you keeping your json file? I puted that file in public folder and when code is:

fetch(shop.json', { method:"get", headers:{ 'Accept': 'application/json', 'Content-Type': 'application/json' } }) .then(response => { return response.json() }) .then(data => { console.log(data) });

2017-11-13 16:58 GMT+00:00 Matt notifications@github.com:

Ok so I created a js file and put the following in it. var jsonfile = require('jsonfile') var file = './exampledata.json' jsonfile.readFile(file, function(err, obj) { console.dir(obj) })

when I run node myjsonreader.js I get the data { id: 1, name: 'A green door', price: 12.5, tags: [ 'home', 'green' ] }

but when I try and bring it in to the app with your fetch I get: SyntaxError: Unexpected token < in JSON at position 0 at line: return response.json()

I see how it is working from the js file but how do i get that into my react app now?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jprichardson/node-jsonfile/issues/97#issuecomment-343984728, or mute the thread https://github.com/notifications/unsubscribe-auth/AcZVpRBTcUXW1zdUnKdCoWepgzleTT1Dks5s2HVNgaJpZM4QZr9g .

Sarkani commented 6 years ago

fetch('shop.json'

johndobrien commented 6 years ago

@iiidefektiii if you're trying to do this in the browser, it won't work. You'll need to use an ajax call. This is the wrong library for doing this in the browser.

iiidefektiii commented 6 years ago

@Sarkani aren't you just fetching the json file at that point? I don't see how your using jsonfile that way. @webm0nk3y Then how do you go about using it if its not able to in the browser? I can get the data from command line. But I need to pull the data into my react app. So what's the use for this package if you can't get it into the browser?

Sarkani commented 6 years ago

Yes I do. And with that method I getting data from that file, Of course is just for reading. You cant update your file in this way. And that packed how I understand its suppose to be used in server side not in browser. In server side its working without any problems

johndobrien commented 6 years ago

@iiidefektiii You'll need a web service running somewhere with an API. I would recommend starting with some basic tutorials on developing a web application and then move up to react. Beyond that, sorry I can't help.

iiidefektiii commented 6 years ago

I've done web stuff. This package is suppose to "make it easier" but I feel like its making it more complicated. I can get everything else to work other methods but nothing with this package. @webm0nk3y You're edited answer makes more sense.

iiidefektiii commented 6 years ago

I ended up getting things to do what I wanted to do. I went a different route thinking this was easier and ended up writing a php script to gather my data from a db, convert it to a json format and pull it into my react app where I can use the data.

Sorry for the confusion. This package was a little misleading in the fact that it never states that it cannot be pulled into react via the browser.

This issue can be closed as it is not an issue as much as it was confusion. @RyanZim @jprichardson

jprichardson commented 6 years ago

This package was a little misleading in the fact that it never states that it cannot be pulled into react via the browser.

While we could be explicit about everything this package does not do, I'd think the execution environment is pretty clear given the package name prefix node- and the first header containing Node.js: https://github.com/jprichardson/node-jsonfile#nodejs---jsonfile

If anyone agrees that we need to be more explicit, I'm more than happy to. Please submit a PR.

iiidefektiii commented 6 years ago

@jprichardson that sounds better. I don't think you need to explicit about everything other then this isn't going to read the data into a browser if that's not what it does. At least people will differentiate with just one short sentence and save issues and confusion.

JoshuaQYH commented 5 years ago

My solution about this issue is importing the json file named team.json as an object.

My json file is like this.

    "abi": ...
    "bytecode": ...

In the App.js, I import json file with the sentence: import Team from './team.json';

And I can work this:

const teamContract = new web3.eth.Contract(Team.abi, teamAddress, {
        from: accounts[0],
        gas: '4700000'
      });

My node version is : v8.11.3. Maybe it can work for you.