Replies: 3 comments 1 reply
-
|
Yes this is intentional behavior. Since your code should remain portable, We might change this for Nitro v3 if there are usecases for it. if you need to read assets, I would suggest to use |
Beta Was this translation helpful? Give feedback.
-
|
I meet the same issue. I want to minify the server bundle and wrap them to a single file instead of using nitro's default bundling strategy: nitro: {
minify: false,
sourceMap: !isProd,
noExternals: isProd,
commonJS: {
ignoreDynamicRequires: isProd,
},
rollupConfig: {
plugins: [esmShim()],
external: ['bufferutil', 'utf-8-validate'],
},
},The import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);But nitro replaces Maybe nitro v3 could provide a solution for such usecase |
Beta Was this translation helpful? Give feedback.
-
|
I encountered the same issue. I'm using Prisma import * as process from 'node:process'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))Everything works fine in development mode, After some digging, I found that even though index.mjs defines this at the top: It doesn't get applied properly because nitro.mjs runs before index.mjs, As a result, the Prisma client ends up resolving __dirname using "file:///_entry.js", which causes the error. My workaround was to use a polyfill: scripts/polyfill.mjs And run with: I'm not sure if index.mjs is supposed to run first or if this is unintended behavior, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Environment
Node.js v18.20.4
Nitropack 2.11.4
Reproduction
npx giget@latest nitro nitro-app --installcd nitro-appserver/plugins/test.tsand paste:npm run buildnode .output/server/index.mjsDescribe the bug
Hello, I am currently building locally a Nuxt project and then I run
node --inspect .output/server/index.mjsin order to inspect the process. The server is build with Nitro. I have in the generated.output/server/chunks/runtime.mjssome reference to file system files using__dirname.As we are in ES module,
__dirnameis deduced as:But in the built bundle,
import.metais transformed by Rollup intoglobalThis._importMeta_stub that ends up withurlequal to"file:///_entry.js"according to this line of code in the Nitro plugin (isEntryisfalsefor this file):nitro/src/rollup/plugins/import-meta.ts
Line 20 in 14283fb
So
__dirnameis acutally/which is the root of my computer, not the root of the directory on which I run the process.This is a problem because the process does not have rights to write into the root of the computer (please don't ask why I'm not root of my computer in my company), but anyway it should not write there.
I made a simple reproduction in Nitro JS directly.
If I edit the generated
.output/server/chunks/runtime.mjsand replace stuburlwithimport.meta.url, everything works as expected.Additional context
No response
Logs
Beta Was this translation helpful? Give feedback.
All reactions