Preset cloudflare-pages/cloudflare-module not working with @aws-sdk
Environment
Nuxt 3.8.0 with Nitro 2.7.2 vite v4.5.0
Reproduction
https://codesandbox.io/p/github/unjs/nitro/csb-z43zd7/draft/tender-wu
- Run "Nitro Build" Task from the "Run Task or Terminal" button
- Run "Wrangler" Task 3.-> observe following error when the code is trying to load the @aws-sdk
[nitro] [request error] [unhandled] Cannot convert object to primitive value
at e2.defaultUserAgent (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:13983:77)
at Um.getRuntimeConfig (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:15730:388)
at new PollyClient (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:15755:44)
at file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:16681:14
at file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:2318:26
at Object.handler (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:3428:54)
at Object.handler (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:2343:29)
at async file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:2420:7
at async file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:3451:20
at async jsonError (file:.//project/nitro/.wrangler/tmp/dev-mFkcA3/dmwkb6o22td.js:16695:12)
Describe the bug
Can not use AWS-SDK with cloudflare-pages/cloudflare-module Nitro Preset and using wrangler to deploy to Cloudflare, always getting errors from AWS-SDK.
I found a somewhat related issue but don't understand how to workaround: https://github.com/cloudflare/workers-sdk/issues/2081
Additional context
No response
Logs
No response
Most likely related to or even a dupe of https://github.com/unjs/unenv/issues/86 -> https://github.com/unjs/unenv/issues/92
I think cloudflare doesn't support aws-sdk. Try using aws4fetch instead.
@cosbgn cloudflare supports aws-sdk, I can send emails in a simple worker using script below.
But I can't do it in the Nuxt / Nitro event handler, cloudflare throws an error.
@manniL issues you mentioned were closed in unjs/unenv v1.9.0... Any idea why SESClient still throws an error:
Cannot convert object to primitive value
import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses'
export default eventHandler(async () => {
const message = {
Subject: { Data: 'subject' },
Body: { Html: { Data: 'body' } },
}
const mail = new SendEmailCommand({
Source: '[email protected]',
ReturnPath: '[email protected]',
Destination: { ToAddresses: ['[email protected]'] },
Message: message,
})
const credentials = {
accessKeyId: '***',
secretAccessKey: '***',
}
const client = new SESClient({
region: 'eu-central-1',
credentials,
});
const send_mail = await client.send(mail).catch(console.error)
return send_mail ? { message: 'Email sent' } : { message: 'Error' }
})
Cannot convert object to primitive value
at e.defaultUserAgent (chunks/index.mjs:1:394301)
at gp.getRuntimeConfig (chunks/index.mjs:1:487967)
at new SESClient (chunks/index.mjs:1:489918)
at chunks/test.mjs:1:347
at chunks/nitro/cloudflare-pages.mjs:1:62477
at async Object.handler (chunks/nitro/cloudflare-pages.mjs:1:62913)
at async chunks/nitro/cloudflare-pages.mjs:1:65282
at async chunks/nitro/cloudflare-pages.mjs:1:104325
@cosbgn cloudflare supports
aws-sdk, I can send emails in a simple worker using script below.But I can't do it in the Nuxt / Nitro event handler, cloudflare throws an error.
Agree, there's a working example from Cloudflare here.
Most likely related to or even a dupe of unjs/unenv#86 -> unjs/unenv#92
I don't think it's the case since I'm still having this issue.
@leogargioni do you mind making a recent repro? What is latest issue/error you get?
@pi0 sure, thanks for looking into it.
I've created this repo with api examples:
- /api/test is a simple check to ensure it is able to get environment variables and working when @aws-sdk is not being used
- /api/test1 is the way I prefer to use, but having 500 error in Cloudflare: "Cannot convert object to primitive value"
- /api/test2 is trying to adapt the working example for workers into nuxt, but also having 500 error in Cloudflare: "global is not defined"
For /api/test1, the log in Cloudflare is:
"[nuxt] [request error] [unhandled] [500]", "Cannot convert object to primitive value\n at defaultUserAgent (chunks//index.mjs:1:314339) \n at Id.getRuntimeConfig (chunks//index.mjs:1:519815) \n at new DynamoDBClient (chunks//index.mjs:1:521815) \n at chunks//ddbClient.mjs:1:206"
Please note that all tests work in dev (npm run dev), but when trying to preview the test1 and test2 also don't work, but locally (with wrangler) the error is "TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute" for both endpoints.
To test, you'll need to have a table called "Example" in DynamoDB, I've deployed the local version of DynamoDB to test it locally in development environment. To create the "Example" table in local DynamoDB run the following command:
aws dynamodb create-table --table-name Example --attribute-definitions AttributeName=PK,AttributeType=S --key-schema AttributeName=PK,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 --table-class STANDARD --region=sa-east-1 --endpoint-url http://localhost:8000/
Lastly, FYI, I've followed this guide to create the Nuxt app and deploy in Cloudflare.
Please let me know if you need more info. Thanks.