Environment detection: node.js or browser

后端 未结 8 1957
误落风尘
误落风尘 2020-11-29 20:54

I\'m developping a JS-app that needs to work both on the client side and the server side (in Javascript on a browser and in Node.js), and I would like to be able to reuse th

8条回答
  •  再見小時候
    2020-11-29 21:38

    There is an npm package just for this and it can be used both on client-side and server-side.

    browser-or-node

    You can use it this way

    if (isBrowser) {
      // do browser only stuff
    }
    
    if (isNode) {
      // do node.js only stuff
    }
    

    Disclaimer: I am the author of this package :)

提交回复
热议问题