Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import.meta.resolve is broken in vite-node, despite working in node #6953

Open
6 tasks done
GauBen opened this issue Nov 22, 2024 · 4 comments
Open
6 tasks done

import.meta.resolve is broken in vite-node, despite working in node #6953

GauBen opened this issue Nov 22, 2024 · 4 comments

Comments

@GauBen
Copy link

GauBen commented Nov 22, 2024

Describe the bug

Given an index.mjs file with the following line of code:

console.log(import.meta.resolve("./index.mjs"));

It works properly when running node index.mjs, but throws an error when running in vite-node npx vite-node index.mjs: TypeError: __vite_ssr_import_meta__.resolve is not a function

Reproduction

  1. mkdir repro && cd $_
  2. echo 'console.log(import.meta.resolve("./index.mjs"));' > index.mjs
  3. node index.mjs will print file://.../repro/index.mjs
  4. npx vite-node index.mjs will print
Need to install the following packages:
[email protected]
Ok to proceed? (y)

/home/gautier/repro/index.mjs:1
'use strict';async (__vite_ssr_import__,__vite_ssr_dynamic_import__,__vite_ssr_exports__,__vite_ssr_exportAll__,__vite_ssr_import_meta__,require,exports,module,__filename,__dirname)=>{{console.log(__vite_ssr_import_meta__.resolve("./index.mjs"));

                                                                                                      ^

TypeError: __vite_ssr_import_meta__.resolve is not a function
    at /home/gautier/repro/index.mjs:1:25
    at ViteNodeRunner.runModule (file:///home/gautier/.npm/_npx/f2342a4b64a2bc92/node_modules/vite-node/dist/client.mjs:399:11)
    at ViteNodeRunner.directRequest (file:///home/gautier/.npm/_npx/f2342a4b64a2bc92/node_modules/vite-node/dist/client.mjs:381:16)
    at ViteNodeRunner.cachedRequest (file:///home/gautier/.npm/_npx/f2342a4b64a2bc92/node_modules/vite-node/dist/client.mjs:206:14)
    at ViteNodeRunner.executeFile (file:///home/gautier/.npm/_npx/f2342a4b64a2bc92/node_modules/vite-node/dist/client.mjs:169:12)
    at CAC.run (file:///home/gautier/.npm/_npx/f2342a4b64a2bc92/node_modules/vite-node/dist/cli.mjs:99:5)

Node.js v20.18.0

System Info

System:
    OS: Linux 5.15 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (16) x64 AMD Ryzen 7 4800HS with Radeon Graphics
    Memory: 3.52 GB / 7.47 GB
    Container: Yes
    Shell: 5.9 - /home/linuxbrew/.linuxbrew/bin/zsh
  Binaries:
    Node: 20.18.0 - ~/.pkgx/nodejs.org/v20.18.0/bin/node
    npm: 10.9.1 - ~/.pkgx/npmjs.com/v10.9.1/bin/npm
    vite-node: 2.1.5

Used Package Manager

npm

Validations

@hi-ogawa
Copy link
Contributor

There was some discussions in the past (and my attempt #5188), but unfortunately we cannot implement it properly on vite-node / Vitest currently.

@GauBen
Copy link
Author

GauBen commented Nov 26, 2024

Thanks @hi-ogawa! By any chance, is Vite 6 bringing the team one step closer to implementing import.meta.resolve?

@sheremet-va
Copy link
Member

Thanks @hi-ogawa! By any chance, is Vite 6 bringing the team one step closer to implementing import.meta.resolve?

No, import.meta.resolve is still not available even in Vite Module Runner

@aklinker1
Copy link

aklinker1 commented Nov 28, 2024

I was able to work around this, but it's not pretty. In my specific case, it's an E2E test, so I want to have it actually resolve the path to the requested module.

// vitest.config.ts
export default defineConfig({
  test: {
    setupFiles: ["./vitest.setup.ts"],
  },
});
// vitest.setup.ts
import { vi } from 'vitest';
import { createRequire } from 'node:module';

vi.stubGlobal('globalCreateRequire', createRequire);
// source file that uses `import.meta.resolve`...

// ...

// At the very bottom:
if (typeof __vite_ssr_import_meta__ !== 'undefined') {
  __vite_ssr_import_meta__.resolve = (path: string) =>
    'file://' + globalCreateRequire(import.meta.url).resolve(path);
}

We create globalCreateRequire in the setup file so we don't have to add an import for createRequire in the source-code.

You have to put that footer in every source file that uses import.meta.resolve, because it seems like the __vite_ssr_import_meta__ is defined per-file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants