kriszyp/cbor-x

Console warning intended for browsers may be printed in NodeJS

jloleysens opened this issue · 2 comments

Hello! Thank you for the great library!

I am seeing the following warning in NodeJS:

  console.warn
    For browser usage, directly use cbor-x/decode or cbor-x/encode modules. The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received 'http://blah/node.cjs'

I have been using this library in an environment where the window object is polyfilled by a test environment (JSDOM) for Jest-based tests. For complicated reasons, we cannot really alter the Jest environment to suppress the message.

Maybe something like could work as the guard for the warning:

   // taken from https://github.com/flexdinesh/browser-or-node/blob/ae67a84b7cdc65021a198b18f16c3bdbf4b480d8/src/index.js
    const isBrowser =
      typeof window !== "undefined" && typeof window.document !== "undefined";

    const isNode =
      typeof process !== "undefined" &&
      process.versions != null &&
      process.versions.node != null;
    
    isBrowser && !isNode

I don't have a solid alternative. Given that names on the global space are not reserved and window might exist in a NodeJS environment 😢 .

Let me know what you think!

I removed the warning. I don't think it is really necessary anymore.

Great! Thanks 👊🏻