Comments (8)
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 } },
- In webpack.config.js:
from osc-js.
This also happens with webpack in the browser, as osc-js thinks its node when __dirname is set, see
Line 1 in d5693c6
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.
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.
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:
- Introducing a
browser
field inpackage.json
which points at the special browser buildosc.min.js
- Replace
dgram
andws
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.
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.
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.
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.
Brilliant, thanks!
from osc-js.
Related Issues (20)
- Accept full URL in osc.open() HOT 1
- Uncaught ReferenceError: OSC is not defined HOT 1
- Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
- Connecting to another device on the network fails with host: localhost. need 0.0.0.0
- Browser(http) osc message to Reaper(udp) HOT 4
- Use TypeScript HOT 4
- Implement booleans HOT 5
- Integer bundle timetags ignored HOT 3
- Use another documentation generator
- Export `Message` class (and possibly other classes)
- ERR_SOCKET_DGRAM_NOT_RUNNING HOT 1
- Can we avoid using websocket and use the library in the browser? HOT 2
- From maxmsp to browser HOT 1
- TypeScript compile error because of incompatible override of Atomic.unpack by its subclasses HOT 2
- Event listener on server on client connect
- Trouble sending osc message to UDP client in LAN network HOT 1
- Handling errors in unpack in Node.js HOT 2
- Nested bundles cannot be processed
- http package is deprecated, so I cannot install osc-js. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from osc-js.