bun
bun copied to clipboard
bunx --bun vite build error during build: [vite:css] import.meta is only valid inside modules.
What version of Bun is running?
1.1.39
What platform is your computer?
Darwin 23.6.0 arm64 arm
What steps can reproduce the bug?
- bun create vite my-app
- cd my-app
- pnpm install && pnpm install less
- bunx --bun vite build
package.json
{
"name": "my-app",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"bun:build": "bunx --bun vite build"
},
"devDependencies": {
"vite": "^6.0.3"
},
"dependencies": {
"less": "^4.2.1"
}
}
app.less -> body { background-color: aliceblue;} main.js -> import './app.less'
What is the expected behavior?
What do you see instead?
bun bun:build
$ bunx --bun vite build
vite v6.0.3 building for production...
✓ 6 modules transformed.
x Build failed in 49ms
error during build:
[vite:css] import.meta is only valid inside modules.
file: /Users/user/code/code/my-app/src/app.less
at
Additional information
This problem does not exist in vite 5.x, it only appears after upgrading vite to 6.x
I can also reproduce this issue. Running Vite with Node works fine.
I can also reproduce this issue + 1
I can also reproduce this issue .
Running vite works fine but bunx --bun vite fails
Reproduced on bun 1.1.42 and vite 6.0.5
I think issues related to Vite need to be taken seriously, as many people use Bun to run their build scripts.
No known workaround other than downgrading vite to v5. So this also limits bun for nuxt users, as nuxt 3.15 is bundled with vite 6.
For me Vite 6 works on bun v1.1.37. I want to use the latest bun, but settled for no Bun upgrades till this is sorted out
+1 Vite v6.0.7 | bun v1.1.42 Any news?
P.S. for me it does not work on v1.1.37 as nick4814 mentioned
it seems a new version of vite uses Function.prototype.toString(), then passing the result of this to new Function(). the input file gets run through the transpiler, converting require to import.meta.require, which is not valid code when run through new Function. this only happens when in an ES module.
this isnt exactly what vite is doing, but this example shows it reasonably:
function hello() {
return typeof require('fs') === 'string' ? 'PASS' : "FAIL";
}
const newFunctionBody = `return ${hello.toString()}`;
const loadFakeModule = new Function('require', newFunctionBody)((id) => `fake require ${id}`);
console.log(loadFakeModule());
export {}; // force es module transpilation
a reasonable solution for this would be to hoist the require implementation instead of inlining it
// @bun
+const { require } = import.meta;
function hello() {
- return typeof import.meta.require("fs") === "string" ? "PASS" : "FAIL";
+ return typeof require("fs") === "string" ? "PASS" : "FAIL";
}
const newFunctionBody = `return ${hello.toString()}`;
const loadFakeModule = new Function("require", newFunctionBody)((id) => `fake require ${id}`);
console.log(loadFakeModule());
export {};
+1 Vite v6.0.7 | bun v1.1.42 Any news?
P.S. for me it does not work on v1.1.37 as nick4814 mentioned
Maybe it is due to your Vite version? I am on [email protected] with [email protected]
Other than that, strange that it works for me on v1.1.37 but does not for you
I haven't tried with Bun v1.1.43 however, it seems that the issue might be fixed now. I will try it out tomorrow