vite-plugin-node-polyfills icon indicating copy to clipboard operation
vite-plugin-node-polyfills copied to clipboard

TypeError: net.createConnection is not a function

Open Miniontoby opened this issue 1 year ago • 7 comments

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!

Miniontoby avatar Jan 07 '24 19:01 Miniontoby

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

davidmyersdev avatar Jan 14 '24 18:01 davidmyersdev

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);

Miniontoby avatar Jan 14 '24 18:01 Miniontoby

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

RandomGuyInLife avatar Feb 12 '24 03:02 RandomGuyInLife

@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.

davidmyersdev avatar Feb 12 '24 10:02 davidmyersdev

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

RandomGuyInLife avatar Feb 12 '24 15:02 RandomGuyInLife

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 avatar Apr 20 '24 21:04 rzfzr

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/",
    },
  })

rzfzr avatar Apr 20 '24 21:04 rzfzr

Hello, @davidmyersdev ! I did it for you https://codesandbox.io/p/devbox/dank-smoke-s8hd6c

Look at the last console error (HelloWorld.vue:10)

RealGoodProgrammer avatar Oct 25 '24 18:10 RealGoodProgrammer