-1

So im trying to make an update checker that doesnt actually download the update but nvm and all im trying to is check if the version on the package.json on github is the same as the one in the app (im making it in electron)

And with this code i get a "path must be a string error" (heres an image https://gyazo.com/7b55a1cbe96b2719bb588a6591855839 )

also i did look it up many times and in fact the code to get the package.json from github with the http module was from how to require from URL in Node.js

function checkforupdate() {

var http = require('http')

const file_url = "https://raw.githubusercontent.com/FloffahDevelopments/FloffahsHub/master/package.json";
const oldpackagejson = require("package.json");
document.getElementById("checkupdate").innerHTML = 'Checking for updates</br>Please wait</br><img src="./assets/icons/gif/loading.gif" alt="loading" style="width: 10%; left: 45%;">'
http.get(file_url).then(res => res.json()).then(pack => {
    if (pack.version !== oldpackagejson.version) {
        document.getElementById("checkupdate").innerHTML = 'Update available!'
    } else {
        document.getElementById("checkupdate").innerHTML = 'No update available!'
    }
});

}

Floffah
  • 3
  • 5
  • The short answer is, you're getting the error because you're passing a `Promise` to `require()`. –  Feb 28 '18 at 21:00
  • @ChrisG What could i do to fix this? – Floffah Feb 28 '18 at 21:01
  • In your package.json, does it say `"version": "1.2.3"`? Because in that case you're overthinking this; you don't need `require()` at all; just `http.get()` the file, parse it, and read `version`. Also, package.json isn't a module, so the question you linked to (and the code mentioned there) don't apply at all. –  Feb 28 '18 at 21:03
  • @ChrisG oh i didnt know that. Would i just do `if (http,get thing !== newpackagejson.version) { ? – Floffah Feb 28 '18 at 21:05
  • You would use `http.get(file_url).then(res => res.json()).then(pack => { if (pack.version ... ) ...});` –  Feb 28 '18 at 21:06
  • ah, ok. Thanks @ChrisG ! – Floffah Feb 28 '18 at 21:09
  • When i tried to do that it tells me `cannot find module on package.json` does the file_url need to have the https:// or http:// at the start or just the raw.githubusercontent.com url @ChrisG – Floffah Feb 28 '18 at 21:16
  • The url is `https://raw.githubusercontent.com/FloffahDevelopments/FloffahsHub/master/package.json` However, using the line I posted shouldn't cause that error. Put the code you're trying to use in your question. –  Feb 28 '18 at 21:28
  • @ChrisG i edited the code in the question, sorry for the late reply – Floffah Feb 28 '18 at 21:42
  • 1. Don't wrap the entire thing in an `if(...)` 2. You need `pack.version !== oldpackagejson.version` (as should be obvious from the error message you got) –  Feb 28 '18 at 21:44
  • ok i tried all that but i still get a `Uncaught Error: Cannot find module 'package.json'` (il update the code to what it is now) – Floffah Feb 28 '18 at 21:54
  • @ChrisG if you have it, could we maybe discuss this on discord since its tellign me not to use discussions in comments. my discord is @[FloffDevs] ℱ#0001 – Floffah Feb 28 '18 at 22:07
  • Your issue is this: `const oldpackagejson = require("package.json");` See: https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code –  Feb 28 '18 at 22:08
  • @ChrisG ah, simple mistakes cause errors. Thanks for your help! Its really appreciated! – Floffah Feb 28 '18 at 22:10
  • @ChrisG the only thing now is `http.get(...).then is not a function` what do i do – Floffah Feb 28 '18 at 22:15
  • @Floffah You [RTFM](https://nodejs.org/api/http.html#http_http_get_options_callback). It's not a function because `http.get()` does not return a `Promise`. – mingos Feb 28 '18 at 22:56

1 Answers1

0

This will make the request you want:

var https = require('https')
const file_url = "https://raw.githubusercontent.com/FloffahDevelopments/FloffahsHub/master/package.json"
const oldpackagejson = require("./package.json");
https.get(file_url, (res) => {
  res.on('data', (d) => {
    process.stdout.write(d)
  })
}).on('error', (e) => {
    console.log(e)
})

Some mistakes you made were: Treating http.get as a promise when it's not. It could by with the inclusion of a module like bluebird, though. You used the http module to make an https request. You did not give http.get it's parameters correctly, that is, your syntax was incorrect. You're trying to update the DOM on server-side code, you should separate client and server logic. res => res.json() does not change res to json, you'll need JSON.parse.

Monte Roden
  • 61
  • 2
  • 5