GithubHelp home page GithubHelp logo

Comments (8)

tschnz avatar tschnz commented on May 19, 2024 3

This happens when using node.js with webpack due to breaking changes upgrading webpack from v4 to v5. In short: When it comes to polyfills, v4 automatically sets all requires of node.js internals (fs, path, dgram, ...) to false packing for the browser. In v5 we explicitly have to state that there is NO polyfill, otherwise it triggers a warning, leaving osc-js useless.

Solutions

  • Use webpack < v5
  • Use a fallback:
    • In webpack.config.js:
      module.exports = {
        //...
        resolve: {
          fallback: {
           "dgram": false, // do not include a polyfill for dgram,
          },
        },
      };
      
    • or when using the API in a build script:
       const webpackCompiler = webpack({
         // ...
         resolve: {
           fallback: { "dgram": false }
         },
      

from osc-js.

felixroos avatar felixroos commented on May 19, 2024 3

This also happens with webpack in the browser, as osc-js thinks its node when __dirname is set, see

const dgram = typeof __dirname !== 'undefined' ? require('dgram') : undefined
.

The Problem: webpack will set __dirname in the browser too, which results in webpack trying to resolve dgram in the browser, which fails.

Yes, the problem can be avoided with a custom webpack config, but thats not a perfect solution, because sometimes, you will not have direct access to the webpack config. For example in create-react-app (on of the most widely used ways to create frontend projects), you will only get to configure webpack after ejecting, which you would want to avoid.

An easy fix would be to use window or global to differentiate between node and browser, see https://stackoverflow.com/questions/4224606/how-to-check-whether-a-script-is-running-under-node-js

from osc-js.

adzialocha avatar adzialocha commented on May 19, 2024 2

An easy fix would be to use window or global to differentiate between node and browser,

Thank you for this hint @felixroos, I'll look into it now! 😃

from osc-js.

adzialocha avatar adzialocha commented on May 19, 2024 2

That didn't work for me unfortunately. It works if a try-catch is added

Alright, I've made some experiments and think the best solution is to tackle the problem during the build process by:

  1. Introducing a browser field in package.json which points at the special browser build osc.min.js
  2. Replace dgram and ws with custom files during browser builds to have better control than just checking global contexts

It works for me with Webpack / simple static html websites and in Node 👍

See PR here: #63 I'll do some more testing and merge, release this soon!

from osc-js.

yaxu avatar yaxu commented on May 19, 2024 1

That didn't work for me unfortunately. It works if a try-catch is added, like:

  var dgram;
  try {
    dgram = require('dgram');
  } catch(error) {
    if (typeof window === 'undefined') {
      throw(error);
    }
  }

... and the same for dgram$1.

However, it doesn't seem to be working for @felixroos even then..

from osc-js.

adzialocha avatar adzialocha commented on May 19, 2024 1

This should be fixed now in v2.3.2. Let me know if the problem still persists, then I'll re-open this issue 👍

from osc-js.

adzialocha avatar adzialocha commented on May 19, 2024

I've published a fix in release v2.3.1, could you tell me if this works for you @felixroos and @schlenger?

from osc-js.

yaxu avatar yaxu commented on May 19, 2024

Brilliant, thanks!

from osc-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.