Vite: [vite] Dep optimization failed with error: Could not load stream ENOENT: no such file or directory

Created on 24 Dec 2020  路  8Comments  路  Source: vitejs/vite

Describe the bug

Vite unable to run after installing package from npm in brand new repository.

"C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" run dev --scripts-prepend-node-path=auto

> [email protected] dev C:\Users\user\Documents\Programming\test-app
> vite

[vite] Optimizable dependencies detected:
vue, xml-js

[vite] Dep optimization failed with error:
Could not load stream (imported by node_modules\sax\lib\sax.js): ENOENT: no such file or directory, open 'C:\Users\user\Documents\Programming\test-app\stream'
[Error: Could not load stream (imported by node_modules\sax\lib\sax.js): ENOENT: no such file or directory, open 'C:\Users\user\Documents\Programming\test-app\stream'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Users\\user\\Documents\\Programming\\test-app\\stream',
  watchFiles: [
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\index.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\vue\\dist\\vue.runtime.esm-bundler.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\@vue\\runtime-dom\\dist\\runtime-dom.esm-bundler.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\@vue\\shared\\dist\\shared.esm-bundler.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\@vue\\runtime-core\\dist\\runtime-core.esm-bundler.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\@vue\\reactivity\\dist\\reactivity.esm-bundler.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\json2xml.js',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\json2xml.js?commonjs-proxy',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\js2xml.js?commonjs-proxy',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\js2xml.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\xml2json.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\xml2js.js',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\xml2json.js?commonjs-proxy',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\xml2js.js?commonjs-proxy',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\options-helper.js?commonjs-proxy',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\options-helper.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\array-helper.js',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\array-helper.js?commonjs-proxy',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\sax\\lib\\sax.js?commonjs-proxy',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\sax\\lib\\sax.js',
    '\u0000commonjsHelpers.js',
    'stream',
    '\u0000stream?commonjs-proxy',
    '\u0000string_decoder?commonjs-proxy'
  ]
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: `vite`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\user\AppData\Roaming\npm-cache\_logs\2020-12-24T17_03_20_289Z-debug.log

Process finished with exit code 1

Reproduction

npm init vite-app test-app
cd test-app
npm install --save xml-js
npm run dev

System Info

  • required vite version: 1.0.0-rc.13
  • required Operating System: Windows 10
  • required Node version: v12.10.0
  • Optional:

    • npm/yarn version: 6.10.3

    • Installed vue version (from yarn.lock or package-lock.json): 3.0.4

    • Installed @vue/compiler-sfc version: 3.0.4

Logs (Optional if provided reproduction)

  1. Run vite or vite build with the --debug flag.
  2. Provide the error log here.
"C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" run dev --scripts-prepend-node-path=auto

> [email protected] dev C:\Users\user\Documents\Programming\test-app
> vite --debug

  vite:config env mode: development +0ms
  vite:config env: {} +3ms
  vite:resolve (node_module entry) vue -> dist/vue.runtime.esm-bundler.js +0ms
  vite:optimize optimizing vue (imports sub dependencies) +0ms
  vite:resolve (node_module entry) xml-js -> lib/index.js +3ms
  vite:optimize optimizing xml-js (no exports, likely commonjs) +2ms
[vite] Optimizable dependencies detected:
vue, xml-js
Pre-bundling them to speed up dev server page load...
(this will be run only when your dependencies have changed)

[vite] Dep optimization failed with error:
Could not load stream (imported by node_modules\sax\lib\sax.js): ENOENT: no such file or directory, open 'C:\Users\user\Documents\Programming\test-app\stream'
[Error: Could not load stream (imported by node_modules\sax\lib\sax.js): ENOENT: no such file or directory, open 'C:\Users\user\Documents\Programming\test-app\stream'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\Users\\user\\Documents\\Programming\\test-app\\stream',
  watchFiles: [
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\vue\\dist\\vue.runtime.esm-bundler.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\index.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\@vue\\runtime-dom\\dist\\runtime-dom.esm-bundler.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\@vue\\shared\\dist\\shared.esm-bundler.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\@vue\\runtime-core\\dist\\runtime-core.esm-bundler.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\@vue\\reactivity\\dist\\reactivity.esm-bundler.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\xml2json.js',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\xml2json.js?commonjs-proxy',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\xml2js.js',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\js2xml.js?commonjs-proxy',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\xml2js.js?commonjs-proxy',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\json2xml.js',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\js2xml.js',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\json2xml.js?commonjs-proxy',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\options-helper.js',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\options-helper.js?commonjs-proxy',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\array-helper.js',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\xml-js\\lib\\array-helper.js?commonjs-proxy',
    'C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\sax\\lib\\sax.js',
    '\u0000C:\\Users\\user\\Documents\\Programming\\test-app\\node_modules\\sax\\lib\\sax.js?commonjs-proxy',
    '\u0000commonjsHelpers.js',
    '\u0000stream?commonjs-proxy',
    'stream',
    'string_decoder',
    '\u0000string_decoder?commonjs-proxy'
  ]
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] dev: `vite --debug`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\user\AppData\Roaming\npm-cache\_logs\2020-12-24T17_08_02_371Z-debug.log

Process finished with exit code 1
pending triage

All 8 comments

This also happens when I install the pug package

@pipe01. You should put pug into devDependencies.

@nccurry. The node built-in module is used inside xml-js, but vite is not supported that at now. You can ask the maintainer provider es module bundler.

Other,I think this should can be improved in v2 vesion.

@underfin If I understand what you're saying, this error is being caused by the fact that the xml-js package is not using native ECMAScript module imports (e.g. import defaultExport from "module-name"), but is instead using Node.js require (e.g. const crypto = require('crypto')), and vite only supports ECMAScript module imports. An example of using a require statement in the xml-js codebase can be found here. Do I understand that correctly?

If so, it sounds like this isn't really an issue with vite.

For future internet searchers: You can run VueJS 3 applications using vue-cli instead of vite as documented here

npm install -g @vue/cli # OR yarn global add @vue/cli
vue create hello-vue3
# select vue 3 preset

The vite optimizer can pre-bundle cjs to esm. The issue is xml-js is not be pre-bundled, because it used node built-in module.

As a workaround, you can use vue-cli if you use xml-js in broswer.

@nccurry. You can see a workaround in here https://github.com/vitejs/vite/issues/1264.It use rollup-plugin-node-builtins to resolve this issue.

@underfin I don't think that's a workaround, it's a feature request, which cannot be achieved for now.

Yeah.It will be work for this issue after support that feature.

Was this page helpful?
0 / 5 - 0 ratings