James Newton
James Newton

Reputation: 7114

Detect if web app is running in NWJS

I am creating a web app which I plan to deliver both from a standard web site and as an NWJS standalone application. In NWJS, I can use the require command, but not in a browser. I can wrap my command in an if statement:

<script>
  if (typeof require === "function") {require('nwjs-osx-menu')(window)}
</script>

Are there other ways that would be considered better practice?


EDIT: following @Kuf's suggestion

<script>
  if (typeof require === "function") {
    try{
      require('nwjs-osx-menu')(window)
    } catch (error) {}
  }
</script>

Upvotes: 4

Views: 2754

Answers (3)

jon
jon

Reputation: 1828

process.__nwjs if nwjs exist, the value will 1

enter image description here

Upvotes: 2

Lee Goddard
Lee Goddard

Reputation: 11183

My limited understanding is that if process is defined, you are in NWJS, and have access to fields such as process.versions['nw-flavor'].

Upvotes: 0

Kuf
Kuf

Reputation: 17846

Update

If the only difference is loading that library then your code seems fine.

original post

There is a web client require module so that might be a bit risky. I used the following:

function is_nwjs(){
    try{
        return (typeof require('nw.gui') !== "undefined");
    } catch (e){
        return false;
    }
}

Upvotes: 2

Related Questions