axe-me / vite-plugin-node

Vite plugin to run your node dev server with HMR!
1k stars 48 forks source link

Errors with export in `browser-external:...` #48

Closed kasbah closed 1 year ago

kasbah commented 2 years ago

Repro: https://github.com/kasbah/vite-plugin-node-example

When I run:

npx vite

I get

✘ [ERROR] No matching export in "browser-external:child_process" for import "fork"

    node_modules/bullmq/dist/esm/classes/child-pool.js:1:9:
      1 │ import { fork } from 'child_process';
        ╵          ~~~~

✘ [ERROR] No matching export in "browser-external:util" for import "promisify"

    node_modules/bullmq/dist/esm/classes/child-processor.js:1:9:
      1 │ import { promisify } from 'util';
        ╵          ~~~~~~~~~

✘ [ERROR] No matching export in "browser-external:crypto" for import "createHash"

    node_modules/bullmq/dist/esm/commands/script-loader.js:1:9:
      1 │ import { createHash } from 'crypto';
        ╵          ~~~~~~~~~~

✘ [ERROR] No matching export in "browser-external:util" for import "promisify"

    node_modules/bullmq/dist/esm/commands/script-loader.js:5:9:
      5 │ import { promisify } from 'util';
        ╵          ~~~~~~~~~

✘ [ERROR] No matching export in "browser-external:events" for import "EventEmitter"

    node_modules/bullmq/dist/esm/classes/redis-connection.js:1:9:
      1 │ import { EventEmitter } from 'events';
        ╵          ~~~~~~~~~~~~

✘ [ERROR] No matching export in "browser-external:util" for import "debuglog"

    node_modules/bullmq/dist/esm/classes/job.js:3:9:
      3 │ import { debuglog } from 'util';
        ╵          ~~~~~~~~

✘ [ERROR] No matching export in "browser-external:events" for import "EventEmitter"

    node_modules/bullmq/dist/esm/classes/queue-base.js:1:9:
      1 │ import { EventEmitter } from 'events';
        ╵          ~~~~~~~~~~~~

✘ [ERROR] No matching export in "browser-external:crypto" for import "createHash"

    node_modules/bullmq/dist/esm/classes/repeat.js:2:9:
      2 │ import { createHash } from 'crypto';
        ╵          ~~~~~~~~~~

✘ [ERROR] No matching export in "browser-external:events" for import "EventEmitter"

    node_modules/bullmq/dist/esm/classes/compat.js:21:9:
      21 │ import { EventEmitter } from 'events';
         ╵          ~~~~~~~~~~~~

✘ [ERROR] No matching export in "browser-external:events" for import "EventEmitter"

    node_modules/bullmq/dist/esm/classes/flow-producer.js:
cerinoligutom commented 2 years ago

Also getting this issue as well with graphql-tools

✘ [ERROR] No matching export in "browser-external:path" for import "join"

    node_modules/@graphql-tools/load-files/index.mjs:3:9:
      3 │ import { join, extname } from 'path';
        ╵          ~~~~

✘ [ERROR] No matching export in "browser-external:path" for import "extname"

    node_modules/@graphql-tools/load-files/index.mjs:3:15:
      3 │ import { join, extname } from 'path';
        ╵                ~~~~~~~

✘ [ERROR] No matching export in "browser-external:fs" for import "readFileSync"

    node_modules/@graphql-tools/load-files/index.mjs:4:9:
      4 │ import { readFileSync, promises, statSync } from 'fs';
        ╵          ~~~~~~~~~~~~

✘ [ERROR] No matching export in "browser-external:fs" for import "promises"

    node_modules/@graphql-tools/load-files/index.mjs:4:23:
      4 │ import { readFileSync, promises, statSync } from 'fs';
        ╵                        ~~~~~~~~

✘ [ERROR] No matching export in "browser-external:fs" for import "statSync"

    node_modules/@graphql-tools/load-files/index.mjs:4:33:
      4 │ import { readFileSync, promises, statSync } from 'fs';
        ╵                                  ~~~~~~~~

✘ [ERROR] No matching export in "browser-external:module" for import "createRequire"

    node_modules/@graphql-tools/load-files/index.mjs:6:9:
      6 │ import { createRequire } from 'module';
        ╵          ~~~~~~~~~~~~~

✘ [ERROR] No matching export in "browser-external:process" for import "cwd"

    node_modules/@graphql-tools/load-files/index.mjs:7:9:
      7 │ import { cwd } from 'process';
romanzy313 commented 2 years ago

As stated in the docs you need to put these into defineConfig

  optimizeDeps: {
    // Vite does not work well with optionnal dependencies,
    // you can mark them as ignored for now
    // eg: for nestjs, exlude these optional dependencies:
    exclude: [
      "child_process",
      "util",
      "crypto",
      "etc etc",
      "url",
      "events",
    ],
  },
kasbah commented 2 years ago

Thanks @romanzy-1612 I can confirm this fixes it! I would have never guessed this since I would never think of the internal node dependencies as "optional".

Since this plugin is focused on node I move to update the example vite.config.js in the readme with common node internal dependencies in optimizeDeps {exclude: [...]}at the very least.

Even better would be if this plugin could modify the optimizeDeps automatically, is that a possibility?

axe-me commented 1 year ago

with latest vite 4 and latest version of this plugin, you don't need to do the exclude hack anymore for node app.