question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[vite] Internal server error: Failed to resolve import "./chunk-J5JSNRMJ.js" Does the file exist?

See original GitHub issue

Describe the bug

[env] vite: 2.6.14 node: 12.18 vue: 3.2.21 vue-router: 4.0.12

file: node_modules/.vite/vue-router.js?v=a7f4b19c
import { setupDevtoolPlugin } from './chunk-J5JSNRMJ.js'

dev 模式下,在删除 .vite 目录后,冷启动时出现。 多次测试发现有 50% 几率出现这个错误 报错后系统会提示 [vite]: dependencies updated, reloading page…随即页面刷新后恢复正常。

在报错之前会有类似日志出现,表明中间有多次 updating 情况下才会触发问题

[vite] new dependencies found: axios, vue-router , updating...
[vite] Fail to load source Map for node_module/.vite/....chunk-xxx.js
[vite] new dependencies found: axios, vue-router , updating...
[vite] Fail to load source Map for node_module/.vite/....chunk-xxx.js
[vite] Internal server error: ....

Reproduction

  1. remove node_modules/.vite
  2. vite serve
  3. repeat the test

System Info

os: mac 10.15.7
shell: 5.7.1 --/bin/zsh

node: 12.18.3
yarn: 1.22.5
npm: 6.14.6

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
fychinesepjjcommented, Dec 24, 2021

总结 分析了源码后,大概了解到问题是与 vite 更新机制有关系! 如果要根本上解决这个问题,vite 需要改变它的更新机制,目前都是靠定时器进行处理,有重复刷新、编译等问题,很大可能是时序没控制好出现问题。

临时解决方法:提前把所有依赖库都添加到 optimizeDeps(或者external),这个报错问题就不会出现了。

optimizeDeps: {
  include: [
     'vue',
     'vuex',
    'vue-router',
    'lodash-es',
    ....
  ]
}

解决原因分析 服务在第一次启动的时候会预编译所有库依赖。

  if (!middlewareMode && httpServer) {
    let isOptimized = false
    // overwrite listen to run optimizer before server start
    const listen = httpServer.listen.bind(httpServer)
    httpServer.listen = (async (port: number, ...args: any[]) => {
      if (!isOptimized) {
        try {
          await container.buildStart({})
          ---------------------------------------
          // 服务启动的时候预编译
          await runOptimize()
          ---------------------------------------
          isOptimized = true
        } catch (e) {
          httpServer.emit('error', e)
          return
        }
      }
      return listen(port, ...args)
    }) as any
  } else {
     ---------------------------------------
    await container.buildStart({})
    // 服务启动的时候预编译
    await runOptimize()
    ---------------------------------------
  }

潜在问题点分析

  1. 定时 NEW_DEPENDENCY_BUILD_TIMEOUT = 1s 刷新页面,超时后返回 408 让客户端刷新页面
    if (
      server._pendingReload &&
      // always allow vite client requests so that it can trigger page reload
      !req.url?.startsWith(CLIENT_PUBLIC_PATH) &&
      !req.url?.includes('vite/dist/client')
    ) {
      try {
        // missing dep pending reload, hold request until reload happens
        await Promise.race([
          server._pendingReload,
          // If the refresh has not happened after timeout, Vite considers
          // something unexpected has happened. In this case, Vite
          // returns an empty response that will error.
          new Promise((_, reject) =>
             ---------------------------------------------------------------------------------------
            setTimeout(reject, NEW_DEPENDENCY_BUILD_TIMEOUT)
             ---------------------------------------------------------------------------------------
          )
        ])
   
  1. 定时 debounceMs = 100ms ,执行rerun --> optimizeDeps 执行依赖更新
function registerMissingImport(
    id: string,
    resolved: string,
    ssr?: boolean
  ) {
    if (!knownOptimized[id]) {
      currentMissing[id] = resolved
      if (handle) clearTimeout(handle)
---------------------------------
      handle = setTimeout(() => {
        handle = undefined
        rerun(ssr)
      }, debounceMs)
---------------------------------
      if (!server._pendingReload) {
        server._pendingReload = new Promise((r) => {
          pendingResolve = r
        })
      }
    }
  }

上述两个定时器有交叉情况出现,相互影响,最终会导致随机出现之前描述的 internal Error 问题

还有一个不可忽视的问题是依赖库的问题 出现问题都是用到了 lodash-es、ant-design-vue 这类库,他们主要特点是依赖较多,预编译速度也会很慢,不排除每次请求编译过程中,超时等导致出现问题。

0reactions
fychinesepjjcommented, Dec 24, 2021

顺便说下,vite 场景我用到了 virtual module

Read more comments on GitHub >

github_iconTop Results From Across the Web

[vite] Internal server error: Failed to resolve import "XXX" from ...
Does the file exist? Yes it exists! I checked.
Read more >
Ask Question - Stack Overflow
Internal server error : Failed to resolve import "./store" from "src\main.ts". Does the file exist? The code is: import store from "./store";.
Read more >
[vite] internal server error: failed to resolve import - You.com
下午11:07:21 [vite] Internal server error: Failed to resolve import "./banner.svg?component" from "src/pages/test/index.vue". Does the file exist?
Read more >
Troubleshooting - Vite
As Vite does not bundle most of the files, browsers may request many files which ... For example, src/foo.js exists and src/bar.js contains:...
Read more >
How to Set Up Path Resolving in Vite - Morioh
To my surprise, it did not work and I was welcome with this error: Failed to resolve import error. It turns out that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found