N
Nuxt2mo ago
WindOfXaos

Make Nitro build a specific package for browser runtime

I am trying to use @aws-sdk/client-s3 in Cloudflare workers but when the SDK instantiates S3Client it tries to load the configuration from multiple including file system which is not supported and throws fs.readFile is not implemented yet! So after a lot of digging I've came across its package.json and it had these fields
"main": "./dist-cjs/index.js",
"browser": {
"./dist-es/runtimeConfig": "./dist-es/runtimeConfig.browser"
},
"react-native": {
"./dist-es/runtimeConfig": "./dist-es/runtimeConfig.native"
},
"main": "./dist-cjs/index.js",
"browser": {
"./dist-es/runtimeConfig": "./dist-es/runtimeConfig.browser"
},
"react-native": {
"./dist-es/runtimeConfig": "./dist-es/runtimeConfig.native"
},
So I manually modified this require statement in node_modules\@aws-sdk\client-s3\dist-cjs\index.js
// From this
var import_runtimeConfig = require("././runtimeConfig");
// To this
var import_runtimeConfig = require("././runtimeConfig.browser");
// From this
var import_runtimeConfig = require("././runtimeConfig");
// To this
var import_runtimeConfig = require("././runtimeConfig.browser");
To force the package to use the browser implementation which doesn't use fs.readFile Now to my question 😅 I know that server packages are built using nitro which uses rollup I have tried all nitro configurations to change this require string at build time but with no avail. Is there a way to change this string or even instruct nitro to build @aws-sdk/client-s3 for browser environment?
8 Replies
kapa.ai
kapa.ai2mo ago
Hi @WindOfXaos. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the knowledge sources provided, I can see that Nitro does handle module resolution, but the specific configuration you need isn't directly covered in the available documentation. However, I can suggest a few approaches: Using Nitro Plugins You can create a Nitro plugin to modify the runtime behavior. While this won't change the build-time resolution, you might be able to work around the issue:
// server/plugins/aws-sdk-fix.ts
export default defineNitroPlugin((nitroApp) => {
// Your workaround logic here
})
// server/plugins/aws-sdk-fix.ts
export default defineNitroPlugin((nitroApp) => {
// Your workaround logic here
})
Note that you need to explicitly import defineNitroPlugin from nitropack/runtime within your plugin file. Alternative Approach: Use Nitro Configuration While not explicitly documented in the provided sources, Nitro's configuration can be customized through the nitro option in nuxt.config. You can refer to the Nitro configuration docs for more advanced options that might help with module resolution. Recommended Solution For Cloudflare Workers specifically, consider: 1. Using a different S3-compatible client: The knowledge sources mention an S3 driver for unstorage that is "lightweight and based on fetch working with Node.js as well as edge workers." This might be a better fit for edge environments. 2. Module aliasing: While not shown in the provided documentation, Nitro typically supports module aliasing through its configuration. Check the Nitro documentation directly for alias or resolve options that could map the problematic module to its browser version. Unfortunately, the provided knowledge sources don't contain specific information about forcing Nitro to use the browser field from package.json during the build process. You may need to consult the Nitro documentation directly or consider using the unstorage S3 driver as an alternative that's designed for edge environments. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@394936600414519298> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
WindOfXaos
WindOfXaosOP2mo ago
@kapa.ai Solution 1 won't be sufficient, unstorage lacks generating presigned links and other S3 features I am using. Solution 2, I have tried alias option but looks like it only affects top level imports or my source code imports not internal packages imports, plus I cannot find a resolve option for nitro configuration.
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the knowledge sources provided, I understand your constraints. Unfortunately, the available documentation doesn't contain specific information about forcing Nitro to respect the browser field in package.json or modifying internal package imports during the build process. What the Knowledge Sources Show The knowledge sources confirm that: 1. Nitro configuration can be set through the nitro option in nuxt.config, with a reference to Nitro configuration docs 2. There are ESM-related troubleshooting options like build.transpile and alias, but as you've noted, these typically only affect top-level imports Potential Workarounds Since the knowledge sources don't provide a direct solution for your specific case, here are some approaches that might be worth exploring (though not explicitly documented): 1. Build-time patching: Consider using a build tool like patch-package to maintain your manual fix to the @aws-sdk/client-s3 package. While not a Nitro-specific solution, it would persist your changes across installs. 2. Custom Nitro plugin with rollup options: The knowledge sources mention that Nitro uses Rollup internally. You might be able to pass custom Rollup plugins through Nitro's configuration, though this isn't documented in the provided sources. 3. Contact the Nitro team: Given that this is a legitimate use case for edge deployments, this might be worth raising as a feature request or question in the Nitro repository. Unfortunately, the provided knowledge sources don't contain enough information to give you a definitive Nitro-based solution for forcing the browser build of internal package dependencies. You may need to consult the Nitro documentation directly or reach out to the Nitro community for this specific use case. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@394936600414519298> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
WindOfXaos
WindOfXaosOP2mo ago
@kapa.ai Can you show an example nitro plugin that might change this specific resolve path?
var import_runtimeConfig = require("././runtimeConfig");
var import_runtimeConfig = require("././runtimeConfig");
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the knowledge sources provided, I need to clarify that Nitro plugins operate at runtime, not at build time, so they cannot modify how modules are resolved or change require() statements in already-built code. According to the Nitro plugin documentation, Nitro plugins are used to "extend Nitro's runtime behavior" and work with hooks like request, beforeResponse, and afterResponse. They don't have the capability to intercept or modify module resolution. Why This Won't Work The require("././runtimeConfig") statement you're trying to change happens during the module loading phase when Node.js/the runtime resolves dependencies. By the time your Nitro plugin runs, all modules have already been loaded and resolved. What Nitro Plugins Can Do Here's an example from the documentation of what Nitro plugins are designed for:
// runtime/plugin.ts
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('request', (event) => {
console.log('on request', event.path)
})

nitroApp.hooks.hook('beforeResponse', (event, { body }) => {
console.log('on response', event.path, { body })
})
})
// runtime/plugin.ts
export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('request', (event) => {
console.log('on request', event.path)
})

nitroApp.hooks.hook('beforeResponse', (event, { body }) => {
console.log('on response', event.path, { body })
})
})
As you can see, these plugins work with runtime hooks for handling requests and responses, not for modifying module resolution. The Real Solution Unfortunately, the knowledge sources don't contain information about build-time module resolution customization in Nitro. The issue you're facing requires a build-time solution (like Rollup plugins or custom resolution logic), not a runtime plugin. You would need to either: - Use a build-time patching solution (like patch-package) - Configure Nitro's underlying Rollup build with custom plugins (not documented in the provided sources) - Consult the Nitro documentation directly for advanced build configuration options I apologize that the available documentation doesn't provide a direct solution for your specific use case. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@394936600414519298> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?