Skip to content

Errors Thrown When Two Nested Modules Import the Same Module #634

Closed as not planned

Description

@NXY666

Describe the bug

There are three modules involved: a.ts references both b.ts and c.ts, whereas b.ts references c.ts. All of them utilize a module import syntax similar to import * as x from "@/modules/x".

a.ts:

import * as b from "./b";
import * as c from "./c";

export function a() {
	return b.b() + c.c();
}

b.ts:

import * as c from "./c";

export function b() {
	return "b" + c.c();
}

c.ts:

export function c() {
	return "c";
}

At this point, import it in a certain file (for example, background.ts) will result in an error being thrown:

import * as a from '@/modules/a';

export default defineBackground(() => {
	console.log('Hello background!', {id: browser.runtime.id}, a.a());
});
WXT 0.17.12                                                                                                   22:05:26
√ Started dev server @ http://localhost:3000                                                                  22:05:27
i Pre-rendering chrome-mv3 for development with Vite 5.2.10                                                   22:05:27

 ERROR  Cannot find module '@/modules/a'                                                                      22:05:28
Require stack:
- E:\Projects\demo\entrypoints\background.ts

  Require stack:
  - entrypoints\background.ts
  at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
  at Function.resolve (node:internal/modules/helpers:187:19)
  at _resolve (node_modules\jiti\dist\jiti.js:1:251148)
  at jiti (node_modules\jiti\dist\jiti.js:1:253746)
  at E:/Projects/demo/entrypoints/background.ts:35:17
  at evalModule (node_modules\jiti\dist\jiti.js:1:256443)
  at jiti (node_modules\jiti\dist\jiti.js:1:254371)
  at importEntrypointFile (/E:/Projects/demo/node_modules/wxt/dist/cli.js:2278:23)
  at async getBackgroundEntrypoint (/E:/Projects/demo/node_modules/wxt/dist/cli.js:1502:27)
  at async /E:/Projects/demo/node_modules/wxt/dist/cli.js:1306:18

× Command failed after 1.638 s                                                                                22:05:28

 ERROR  Failed to load entrypoint: entrypoints\background.ts                                                  22:05:28

  at importEntrypointFile (/E:/Projects/demo/node_modules/wxt/dist/cli.js:2290:13)
  at async getBackgroundEntrypoint (/E:/Projects/demo/node_modules/wxt/dist/cli.js:1502:27)
  at async /E:/Projects/demo/node_modules/wxt/dist/cli.js:1306:18
  at async Promise.all (index 0)
  at async findEntrypoints (/E:/Projects/demo/node_modules/wxt/dist/cli.js:1294:23)
  at async internalBuild (/E:/Projects/demo/node_modules/wxt/dist/cli.js:3165:23)
  at async buildAndOpenBrowser (/E:/Projects/demo/node_modules/wxt/dist/cli.js:3494:28)
  at async Object.start (/E:/Projects/demo/node_modules/wxt/dist/cli.js:3459:9)
  at async /E:/Projects/demo/node_modules/wxt/dist/cli.js:3980:5
  at async CAC.<anonymous> (/E:/Projects/demo/node_modules/wxt/dist/cli.js:3911:22)

If I remove the import in the background before starting the dev server, and then add it back after the server has started, it does not throw an error.

I am sure the path is correct because import {a} from "@/modules/a" works.

To Reproduce

demo.zip

Steps to reproduce the bug using the reproduction:

  1. Install dependencies: npm i
  2. Start dev mode: npm run dev

Expected behavior

no error

Environment

  System:
    OS: Windows 11 10.0.22631
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i9-13900H
    Memory: 1.83 GB / 15.73 GB
  Binaries:
    Node: 20.11.1 - D:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - D:\Program Files\nodejs\yarn.CMD
    npm: 10.5.2 - D:\Program Files\nodejs\npm.CMD
    pnpm: 8.15.5 - D:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (123.0.2420.97)
    Internet Explorer: 11.0.22621.1

Activity

  1. added
    pending-triage
    on Apr 27, 2024
  2. aklinker1 commented on Apr 28, 2024

    @aklinker1
    Member

    Interesting, this isn't anything special, I must have done this in several of my extensions... I haven't tried out your repro yet, but I will soon. Thanks for the report

  3. aklinker1 commented on May 4, 2024

    @aklinker1
    Member

    Ahh, OK. This is happening during the build, not at runtime. Currently, entrypoints are loaded with jiti during the build to get the entrypoint's options, and this method must not support this for some reason... Not sure why TBH.

    For now, I'd just not use import * as, and use named imports instead. Like you said, it works.

    I'm gonna add this issue to #506 and mark this as a duplicate, it should be fixed once we use vite to load the entrypoint options instead of jiti. But I'll make sure to re-test this once #506 is merged.

  4. added and removed
    pending-triage
    on May 4, 2024
  5. added theissue type on Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions