davidmyersdev / vite-plugin-node-polyfills

A Vite plugin to polyfill Node's Core Modules for browser environments.
MIT License
263 stars 17 forks source link

TypeError: net.createConnection is not a function #70

Closed Miniontoby closed 1 month ago

Miniontoby commented 6 months ago

Hi,

I just started using this plugin.

I get this error:

TypeError: net.createConnection is not a function

Whenever I look at what the import('net') contains, then it doesn't seem to have anything at all...

Please let me know if I do something wrong!

davidmyersdev commented 5 months ago

Hey @Miniontoby. Would you mind adding a reproducible example via Stackblitz or similar? Without it, it's hard for me to debug the problem.

Miniontoby commented 5 months ago

Uhmm, just install discord-rpc npm package and then (try to) use it inside vite.

import { Client } from 'discord-rpc';

const clientId = '700889110440837220';

const rpc = new Client({ transport: 'ipc' });

rpc.login({ clientId }).catch(console.error);
RandomGuyInLife commented 4 months ago

Did this get fixed? I'm having a similar problem.

davidmyersdev commented 4 months ago

@RandomGuyInLife I unfortunately can't dedicate time toward issues without functional reproducible examples. If you're running into a similar error and can provide one, then I can take a look. Based on the error above though, this might just be a case where you need to find a good polyfill override and use the overrides config option.

RandomGuyInLife commented 4 months ago

I'm having trouble with the http module. Is there a good override for that?

rzfzr commented 2 months ago

I'm having the same issue, but I was able to follow the traces:

toggl-api.js?v=e61b10cc:84215 Uncaught TypeError: Cannot read properties of null (reading 'createConnection')
    at node_modules/forever-agent/index.js (toggl-api.js?v=e61b10cc:84215:51)
    at __require2 (chunk-7HH3UP77.js?v=e61b10cc:26:50)
    at node_modules/request/request.js (toggl-api.js?v=e61b10cc:94052:24)
    at __require2 (chunk-7HH3UP77.js?v=e61b10cc:26:50)
    at node_modules/request/index.js (toggl-api.js?v=e61b10cc:95382:23)
    at __require2 (chunk-7HH3UP77.js?v=e61b10cc:26:50)
    at node_modules/toggl-api/lib/client.js (toggl-api.js?v=e61b10cc:100659:19)
    at __require2 (chunk-7HH3UP77.js?v=e61b10cc:26:50)
    at toggl-api.js?v=e61b10cc:100880:25

the forever-agent/index uses an require_empty() method which looks like is being provided by node-stdlib-browser:

// node_modules/node-stdlib-browser/cjs/mock/empty.js
var require_empty = __commonJS({
  "node_modules/node-stdlib-browser/cjs/mock/empty.js"(exports2, module2) {
    "use strict";
    var import_dist = __toESM(require_dist());
    var import_dist2 = __toESM(require_dist2());
    var import_dist3 = __toESM(require_dist3());
    var empty = null;
    module2.exports = empty;
  }
});

I tried overriding the https and http but no luck.,. This is the repo @ the commit with the issue

But I do have the webpack equivalent of a working example, I tried adding the alias too...

rzfzr commented 2 months ago

Ok, so trying to emulate all my webpack configure, what made it work was to EXCLUDE the 'net' module. It is working now, this is my config

nodePolyfills({
    exclude: ['net'],
    overrides: {
      stream: 'stream-browserify',
      https: "https-browserify",
      http: "stream-http",
      util: "util/",
      url: "url/",
      buffer: "buffer/",
    },
  })