question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get it working in Lambda (Cannot find module 'vite-plugin-ssr/dist/cjs/node/index.js')

See original GitHub issue

Hey there ☺️

I’m trying to deploy a Vite React app using vite-plugin-ssr to provide SSR but testing the Lambda via AWS Console results in Cannot find module 'vite-plugin-ssr/dist/cjs/node/index.js' so it seems it’s not correctly bundling the right version or something? I opened an issue at the plugin repo but was told it’s more likely some bundling issue with Pulumi

I’m running

pulumi version => v3.10.3
node -v => v16.5.0

The full error from the AWS Lambda Console:

{
  "errorType": "Error",
  "errorMessage": "Cannot find module 'vite-plugin-ssr/dist/cjs/node/index.js'\nRequire stack:\n- /var/task/__index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Error: Cannot find module 'vite-plugin-ssr/dist/cjs/node/index.js'",
    "Require stack:",
    "- /var/task/__index.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:745:27)",
    "    at Module.require (internal/modules/cjs/loader.js:961:19)",
    "    at require (internal/modules/cjs/helpers.js:92:18)",
    "    at /var/task/__index.js:80:15",
    "    at Object.__f2 [as createPageRender] (/var/task/__index.js:85:6)",
    "    at /var/task/__index.js:105:32",
    "    at Generator.next (<anonymous>)",
    "    at /var/task/__index.js:16:71",
    "    at new Promise (<anonymous>)"
  ]
}

Steps to reproduce

  1. git clone https://github.com/CanRau/pulumi-bundle-issue
  2. yarn
  3. cd www
  4. yarn && yarn build
  5. cd ..
  6. pulumi up to some test environment if possible

Expected: Working Lambda Actual: Error as in title

I use www/server/lambda.ts as Lambda function handler index.ts#L3 Which as mentioned in the plugins docs imports import "../dist/server/importBuild"; Actually lambda.ts itself already imports vite-plugin-ssr.

Maybe I’ve got some tsconfig.json confusions?

Let me know if I can provide anything else 🙏

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
CanRaucommented, Aug 27, 2021

Wow, thanks that’s really interesting, I just cloned my own repo, into a new dir, following above steps, pulumi uped it and voila it completely worked, not even the toLowerCase issue 😳😅 Not yet sure what the difference between the cloned and the original repos is though 🧐

Thank you anyway for taking the time 🙏 I’ll post here if I’ll find out the difference and remember to do so 🤞

0reactions
CanRaucommented, Aug 28, 2021

Uh, the toLowerCase happens probably because I didn’t provide a test event 😅

{
  "body": "",
  "resource": "/{proxy+}",
  "path": "/",
  "httpMethod": "GET",
  "isBase64Encoded": true,
  "queryStringParameters": {},
  "multiValueQueryStringParameters": {},
  "pathParameters": {
    "proxy": "/"
  },
  "stageVariables": {
    "baz": "qux"
  },
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate, sdch",
    "Accept-Language": "en-US,en;q=0.8",
    "Cache-Control": "max-age=0",
    "CloudFront-Forwarded-Proto": "https",
    "CloudFront-Is-Desktop-Viewer": "true",
    "CloudFront-Is-Mobile-Viewer": "false",
    "CloudFront-Is-SmartTV-Viewer": "false",
    "CloudFront-Is-Tablet-Viewer": "false",
    "CloudFront-Viewer-Country": "US",
    "Host": "1234567890.execute-api.us-east-1.amazonaws.com",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Custom User Agent String",
    "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
    "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
    "X-Forwarded-For": "127.0.0.1, 127.0.0.2",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https"
  },
  "multiValueHeaders": {
    "Accept": [
      "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
    ],
    "Accept-Encoding": [
      "gzip, deflate, sdch"
    ],
    "Accept-Language": [
      "en-US,en;q=0.8"
    ],
    "Cache-Control": [
      "max-age=0"
    ],
    "CloudFront-Forwarded-Proto": [
      "https"
    ],
    "CloudFront-Is-Desktop-Viewer": [
      "true"
    ],
    "CloudFront-Is-Mobile-Viewer": [
      "false"
    ],
    "CloudFront-Is-SmartTV-Viewer": [
      "false"
    ],
    "CloudFront-Is-Tablet-Viewer": [
      "false"
    ],
    "CloudFront-Viewer-Country": [
      "US"
    ],
    "Host": [
      "0123456789.execute-api.us-east-1.amazonaws.com"
    ],
    "Upgrade-Insecure-Requests": [
      "1"
    ],
    "User-Agent": [
      "Custom User Agent String"
    ],
    "Via": [
      "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)"
    ],
    "X-Amz-Cf-Id": [
      "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA=="
    ],
    "X-Forwarded-For": [
      "127.0.0.1, 127.0.0.2"
    ],
    "X-Forwarded-Port": [
      "443"
    ],
    "X-Forwarded-Proto": [
      "https"
    ]
  },
  "requestContext": {
    "accountId": "123456789012",
    "resourceId": "123456",
    "stage": "prod",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
    "requestTime": "09/Apr/2015:12:34:56 +0000",
    "requestTimeEpoch": 1428582896000,
    "identity": {
      "cognitoIdentityPoolId": null,
      "accountId": null,
      "cognitoIdentityId": null,
      "caller": null,
      "accessKey": null,
      "sourceIp": "127.0.0.1",
      "cognitoAuthenticationType": null,
      "cognitoAuthenticationProvider": null,
      "userArn": null,
      "userAgent": "Custom User Agent String",
      "user": null
    },
    "path": "/prod/path/to/resource",
    "resourcePath": "/{proxy+}",
    "httpMethod": "POST",
    "apiId": "1234567890",
    "protocol": "HTTP/1.1"
  }
}

Yeah because it’s supposed to be a APIGateway event and I don’t yet verify it and just access toLowerCase on event.httpMethod 😅 And maybe because of this you don’t get the “correct” error as it fails earlier

Read more comments on GitHub >

github_iconTop Results From Across the Web

aws-lambda Cannot find module - Stack Overflow
The way I was able to get this to work was: Name the file exports.js; Name the handler, within the file, exports.handler; Set...
Read more >
Resolve the "Unable to import module" error for Lambda code ...
I receive the "Unable to import module" error when I try to run my AWS Lambda code in Node.js. Short description. You typically...
Read more >
AWS Lambda Error: "Cannot find module '/var/task/index'"
JavaScript : AWS Lambda Error: " Cannot find module '/var/task/ index '" [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] ...
Read more >
Cannot find module (AWS Lambda Error) [Solved] | bobbyhadz
There is a language-specific folder structure you have to follow when using lambda layers, e.g. nodejs/node_modules for Node.js layers.
Read more >
AWS Lambda Function is returning “Cannot find module 'index ...
handler = theHandler();. And yet still does not work with the same error? javascript · node- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found