Runtime.ImportModuleError after uploading my NodeJS project as a zip in AWS Lambda

0

Hope everyone is doing well. When I am trying to deploy my NodeJS (Runtime: 18.0.x) project on AWS Lambda, I am getting the following error:

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'index'\nRequire stack:\n- /var/runtime/index.mjs",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'index'",
    "Require stack:",
    "- /var/runtime/index.mjs",
    "    at _loadUserApp (file:///var/runtime/index.mjs:997:17)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)",
    "    at async start (file:///var/runtime/index.mjs:1195:23)",
    "    at async file:///var/runtime/index.mjs:1201:1"
  ]
}

My folder structure is as follows: Folder Structure My package.json is as follows:

{
  "name": "gpt-simulation-backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "nodemon index.js",
    "start:prod": "NODE_ENV=production nodemon index.js",
    "test": "echo \"Error: no test specified\" && exit 1",
    "lint": "eslint . --ext .js --fix"
  },
  "author": "Srijan Srivastava at Careerhacker",
  "license": "ISC",
  "devDependencies": {
    "eslint": "^8.41.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-import": "^2.27.5",
    "eslint-plugin-jsx-a11y": "^6.7.1",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-prettier": "^4.2.1",
    "nodemon": "^2.0.22",
    "prettier": "^2.8.8"
  },
  "dependencies": {
    "amazon-cognito-identity-js": "^6.3.1",
    "dotenv": "^16.1.3",
    "express": "^4.18.2",
    "express-jsdoc-swagger": "^1.8.0",
    "mongoose": "^7.2.2",
    "morgan": "^1.10.0",
    "node-fetch": "^3.3.1"
  }
}

My index.js is:

import { config } from 'dotenv';
import { connect } from 'mongoose';

import app from './app.js';

config({ path: './.env' });

const dbUri = process.env.MONGODB_URI;

connect(dbUri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
}).then(() => {
  console.log('Connected to MongoDB!!');
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`App running on port ${port}...`);
});

I have tried a lot of things mentioned on stack-overflow and forums to fix the issue. However, the error still persists. The name of the zip is same as that of the name defined in the package.json. Any kind of help is appreciated. I understand that the problem might be answered somewhere, but I have not found a solution which works. If anyone can forward me to a correct resource, I will be glad.

Edit1: I am also selecting files and not the entire folder.

Edit2: Directory structure on AWS (after removing the node_modules) --> AWS Lmbda Folder structure

srijan
已提问 10 个月前2926 查看次数
1 回答
0

Are you selecting a folder when compressing files?
Instead of selecting a folder, please select the files to be compressed one by one for compression.
Compress files under a directory by executing the following command.

zip -r lambda.zip ./*
profile picture
专家
已回答 10 个月前
  • I am selecting the files. Should have mentioned that in the question. My bad.

  • Slightly modified answer. Do you get the same error when you compress with the commands listed in the answer?

  • I still get the same error.

  • Is it possible to show us the directory structure on the Lambda function screen from the management console? Specifically, I want to make sure that the Lambda directory structure is correct as follows. Also make sure that index.js is directly under the root folder at that time. lambda

  • I have updated the question with the folder structure.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则