NuxtN
Nuxtβ€’3mo agoβ€’
14 replies
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"
},


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");

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?
Was this page helpful?