nuxt / vite

โšก Vite Experience with Nuxt 2
https://vite.nuxtjs.org
1.38k stars 46 forks source link

error: [plugin: vite:dep-pre-bundle] Failed to resolve entry for package "fs". #160

Open kanagucalmet opened 3 years ago

kanagucalmet commented 3 years ago

error: [plugin: vite:dep-pre-bundle] Failed to resolve entry for package "fs". The package may have incorrect main/module/exports specified in its package.json while running my exisiting nuxtjs app with vitejs

danielroe commented 3 years ago

@kanagucalmet Could you provide a reproduction?

kanagucalmet commented 3 years ago

๐Ÿงช Vite mode is experimental and some nuxt modules might be incompatible 11:31:17 If found a bug, please report via https://github.com/nuxt/vite/issues with a minimal reproduction. Unstable server-side rendering is enabled

WARN Address 0.0.0.0:5000 is already in use. 11:31:20

โ„น Trying a random port... 11:31:20

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚ โ”‚ โ”‚ Nuxt @ v2.15.4 โ”‚ โ”‚ โ”‚ โ”‚ โ–ธ Environment: development โ”‚ โ”‚ โ–ธ Rendering: server-side โ”‚ โ”‚ โ–ธ Target: server โ”‚ โ”‚ โ”‚ โ”‚ Listening: http://192.168.5.103:39761/ โ”‚ โ”‚ โ”‚ โ”‚ โšก Vite Mode Enabled (v0.1.1) โ”‚ โ”‚ โ”‚ โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

โ„น Preparing project for development 11:31:25 โ„น Initial build may take a while 11:31:25 โœ” Builder initialized 11:31:25 โœ” Nuxt files generated 11:31:27

node_modules/vite/dist/node/chunks/dep-fc2895ac.js:38154:14: error: [plugin: vite:dep-pre-bundle] Failed to resolve entry for package "fs". The package may have incorrect main/module/exports specified in its package.json. 38154 โ”‚ throw new Error(Failed to resolve entry for package "${id}". + โ•ต ^ at resolvePackageEntry (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/vite/dist/node/chunks/dep-fc2895ac.js:38154:15) at tryNodeResolve (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/vite/dist/node/chunks/dep-fc2895ac.js:37973:11) at Context.resolveId (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/vite/dist/node/chunks/dep-fc2895ac.js:37856:28) at Object.resolveId (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/vite/dist/node/chunks/dep-fc2895ac.js:50279:55) at processTicksAndRejections (internal/process/task_queues.js:93:5) at async /home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/vite/dist/node/chunks/dep-fc2895ac.js:74236:27 at async /home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/vite/dist/node/chunks/dep-fc2895ac.js:72051:34 at async callback (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/esbuild/lib/main.js:812:28) at async handleRequest (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/esbuild/lib/main.js:644:30)

node_modules/vite/dist/node/chunks/dep-fc2895ac.js:72038:18: note: This error came from the "onResolve" callback registered here 72038 โ”‚ build.onResolve({ filter: /^[\w@][^:]/ }, async ({ path: id, importer, kind, resolveDir }) => { โ•ต ~~~~~ at setup (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/vite/dist/node/chunks/dep-fc2895ac.js:72038:19) at handlePlugins (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/esbuild/lib/main.js:736:23) at Object.buildOrServe (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/esbuild/lib/main.js:1024:7) at /home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/esbuild/lib/main.js:1750:17 at new Promise () at Object.build (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/esbuild/lib/main.js:1749:14) at Object.build (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/esbuild/lib/main.js:1625:51) at optimizeDeps (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/vite/dist/node/chunks/dep-fc2895ac.js:72268:34) at processTicksAndRejections (internal/process/task_queues.js:93:5) at async runOptimize (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/vite/dist/node/chunks/dep-fc2895ac.js:72852:48) at async Object.createServer (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/vite/dist/node/chunks/dep-fc2895ac.js:72881:9) at async buildClient (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/nuxt-vite/dist/nuxt-vite.js-vite.js:106:22) at async bundle (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/nuxt-vite/dist/nuxt-vite.js-vite.js:459:3) at async Builder.build (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/@nuxt/builder/dist/builder.js:327:5) at async Object._buildDev (/home/kanagu/Desktop/portal/test/vite-nuxt/node_modules/@nuxt/cli/dist/cli-dev.js:107:5)

node_modules/node-gyp-build/index.js:1:17: note: The plugin "vite:dep-pre-bundle" was triggered by this import 1 โ”‚ var fs = require('fs') โ•ต ~~~~

ERROR Build failed with 1 error: 11:31:31 node_modules/vite/dist/node/chunks/dep-fc2895ac.js:38154:14: error: [plugin: vite:dep-pre-bundle] Failed to resolve entry for package "fs". The package may have incorrect main/module/exports specified in its package.json.

node_modules/vite/dist/node/chunks/dep-fc2895ac.js:38154:14: error: [plugin: vite:dep-pre-bundle] Failed to resolve entry for package "fs". The package may have incorrect main/module/exports specified in its package.json. at failureErrorWithLog (node_modules/esbuild/lib/main.js:1449:15) at node_modules/esbuild/lib/main.js:1131:28 at runOnEndCallbacks (node_modules/esbuild/lib/main.js:921:63) at buildResponseToResult (node_modules/esbuild/lib/main.js:1129:7) at node_modules/esbuild/lib/main.js:1236:14 at node_modules/esbuild/lib/main.js:609:9 at handleIncomingPacket (node_modules/esbuild/lib/main.js:706:9) at Socket.readFromStdout (node_modules/esbuild/lib/main.js:576:7) at Socket.emit (events.js:315:20) at Socket.EventEmitter.emit (domain.js:467:12) at addChunk (internal/streams/readable.js:309:12) at readableAddChunk (internal/streams/readable.js:284:9) at Socket.Readable.push (internal/streams/readable.js:223:10) at Pipe.onStreamRead (internal/stream_base_commons.js:188:23)

lukaszarpak commented 3 years ago

Any update on this issue? I experience the same thing

danielroe commented 3 years ago

@lukaszarpak Could you provide a reproduction?

antfu commented 3 years ago

Close due to inactive. If it still persist, please open another issue with proper reproduction.

huchangfa123 commented 3 years ago

have the same problem. image while using this package https://www.npmjs.com/package/css

pi0 commented 3 years ago

Last case for fs, is mostly due to using incompatible (node based) library for client-side bundle, but I think since webpack4 is already stubbing built-ins, we might do the same for for vite.

spencerbug commented 2 years ago

I am also running into this same thing, any updates on this?

jamiehaywood commented 2 years ago

so I had this issue when requiring a mock-service-worker package which is technically isomorphic, but has a bunch of node dependencies (chokidar, node-fetch etc.)

To fix with vite:

  1. yarn add -D rollup-plugin-node-builtins
  2. In your vite.config.ts you need to add the following (I've commented the ones I used and just left fs uncommented for this use case specifically):
    resolve: {
    alias: {
    fs: require.resolve('rollup-pligin-node-builtins'),
    // http: require.resolve('rollup-plugin-node-builtins'),
    // util: require.resolve('rollup-plugin-node-builtins'),
    // stream: require.resolve('rollup-plugin-node-builtins'),
    // buffer: require.resolve('rollup-plugin-node-builtins'),
    // process: require.resolve('rollup-plugin-node-builtins'),
    // url: require.resolve('rollup-plugin-node-builtins'),
    // querystring: require.resolve('rollup-plugin-node-builtins'),
    },
    },
  3. ๐Ÿ’ฐ

EDIT: I realise that this will probably end up being incorporated into Vite, but for those people that came from google who need a quick fix - here you go ๐Ÿš€

ghost commented 2 years ago

After yarn add, I wrote the settings, but I get an error. There seems to be a problem that shebang cannot be read.

[commonjs] Unexpected character '!' 
1: #!/usr/bin/env node
    ^
2: /**
3:  * @license r.js 2.3.6 Copyright jQuery Foundation and other contributors.
degueba commented 2 years ago

Hey guys, any updates on this issue? image

georgeportillo commented 2 years ago

I have the same issue as well:

Screen Shot 2022-04-23 at 5 49 35 PM

Any help with this would be appreciated

MackDevelops commented 2 years ago

same issue as well, using Quasar Framework, my colleague couldn't replicate it as well, tried matching node, npm and package.json versions.

80434a544bcdedfc8ca93092f148c33

Marcos-Montero commented 1 year ago

I had to keep trying to build and add one by one all the node native packages that aws needs in the alias field of the vite.config.ts using the 'rollup-plugin-node-builtins' plugin , this was the result for my project to work:


  resolve: {
    alias: {
      '.runtimeConfig': './runtimeConfig.browser',
      http: require.resolve('rollup-plugin-node-builtins'),
      path: require.resolve('rollup-plugin-node-builtins'),
      fs: require.resolve('rollup-plugin-node-builtins'),
      os: require.resolve('rollup-plugin-node-builtins'),
      tslib: require.resolve('rollup-plugin-node-builtins'),
      child_process: require.resolve('rollup-plugin-node-builtins'),
      crypto: require.resolve('rollup-plugin-node-builtins'),
      stream: require.resolve('rollup-plugin-node-builtins'),
      https: require.resolve('rollup-plugin-node-builtins'),
      http2: require.resolve('rollup-plugin-node-builtins'),
      process: require.resolve('rollup-plugin-node-builtins'),
    },
  },
anish-lakkapragada commented 1 year ago

@jamiehaywood @MarcosMRod, what happens if require is not defined (using module not cjs)? Could be dumb question

Screenshot 2023-01-02 at 22 11 16

do you have to switch to type:"commonjs in package.json?

Even if I use createRequre as recommended here, I still run into issues:

Screenshot 2023-01-02 at 23 38 25

thanks! fyi this is vite used to compile svelte but I think the error is still relevant.

EDIT: My bad sorry, that was because I was trying to use puppeteer (required for node chatgpt package) on client-side. I will move it to the Node API section. Sorry again