orbitdb / field-manual

The Offical User's Guide to OrbitDB
207 stars 43 forks source link

window is not defined in nodejs? #110

Closed cottrell closed 4 years ago

cottrell commented 4 years ago

Should that be in there in the try/cache?:

https://github.com/orbitdb/field-manual/blob/master/01_Tutorial/01_Basics.md

aphelionz commented 4 years ago

Hi @cottrell, are you getting this error?

If you're talking about this code here:

class NewPiecePlease {
  constructor (Ipfs, OrbitDB) {
    this.Ipfs = Ipfs
    this.OrbitDB = OrbitDB
  }
}

try {
    const Ipfs = require('ipfs')
    const OrbitDB = require('orbit-db')

    module.exports = exports = new NewPiecePlease(Ipfs, OrbitDB)
} catch (e) {
    window.NPP = new NewPiecePlease(window.Ipfs, window.OrbitDB)
}

The intention is that the browser will try to parse the try clause, find that require is undefined, and failover to the catch clause.

If that's not happening let me know!

cottrell commented 4 years ago

I coudl be wrong but I think "window" is not defined in node REPL.

aphelionz commented 4 years ago

Testing this:

  1. Made a new directory
  2. npn init --yes
  3. npm install orbit-db ipfs@^0.39.1
  4. node to open the REPL
  5. Pasted the above code block in

Seemed to work fine! Closing this for now but do let me know if your experience is different.

cottrell commented 4 years ago

Did you hit the exception block though? You need to force it. Basically if you type "window" in node it shouldn't work I think.

aphelionz commented 4 years ago

Right but this code is specifically designed to differentiate between environments, essentially like an easy sniff. The idea is that you'd never hit the exception block in node, whereas you would in the browser and hit the catch block.