ReferenceError with lambda function : "errorMessage": "require is not defined in ES module scope

0

const AWS = require('aws-sdk'); const s3 = new AWS.S3();

Response { "errorType": "ReferenceError", "errorMessage": "require is not defined in ES module scope, you can use import instead", "trace": [ "ReferenceError: require is not defined in ES module scope, you can use import instead", " at file:///var/task/index.mjs:1:13", " at ModuleJob.run (node:internal/modules/esm/module_job:218:25)", " at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)", " at async _tryAwaitImport (file:///var/runtime/index.mjs:1008:16)", " at async _tryRequire (file:///var/runtime/index.mjs:1057:86)", " at async _loadUserApp (file:///var/runtime/index.mjs:1081:16)", " at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)", " at async start (file:///var/runtime/index.mjs:1282:23)", " at async file:///var/runtime/index.mjs:1288:1" ] }

asked 2 months ago535 views
1 Answer
0

Hey Kshitij Kumar,

The issue you're encountering is due to a mismatch between module systems in JavaScript, specifically when using Node.js. JavaScript has two main types of module systems: CommonJS (CJS) and ECMAScript Modules (ESM). Each system has its own syntax for importing and exporting modules:

  • CommonJS uses require() for importing and module.exports for exporting. This system is traditionally used in Node.js for server-side code.
  • ECMAScript Modules (ESM) uses import and export syntax. ESM is a newer standard that's used both in the browser and in Node.js for module management.

Your error message "require is not defined in ES module scope, you can use import instead" indicates that your code is being treated as an ES module but is attempting to use CommonJS syntax (require()) for importing modules. This typically happens in one of the following scenarios:

  1. File Extension: Your file has a .mjs extension, which explicitly marks it as an ES module. Node.js treats .mjs files as ES modules by default.

  2. package.json Configuration: Your package.json includes "type": "module", which tells Node.js to treat .js files as ES modules within the scope of that package.

In an ES module context, the require() function is not defined, as ES modules use the import statement for importing modules. This is why the error suggests using import instead of require().

To resolve this issue, you need to conform to the ES module syntax for imports:

import AWS from 'aws-sdk';
const s3 = new AWS.S3();
profile picture
EXPERT
answered 2 months ago
  • Hi @Osvaldo , thank you for ur response, I tried the above, however I am still getting the error : Response { "errorType": "Error", "errorMessage": "Cannot find package 'aws-sdk' imported from /var/task/index.mjs", "trace": [ "Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'aws-sdk' imported from /var/task/index.mjs", " at packageResolve (node:internal/modules/esm/resolve:858:9)", " at moduleResolve (node:internal/modules/esm/resolve:915:20)", " at moduleResolveWithNodePath (node:internal/modules/esm/resolve:1135:12)", " at defaultResolve (node:internal/modules/esm/resolve:1178:79)", " at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)", " at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)", " at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)", " at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)", " at link (node:internal/modules/esm/module_job:84:36)" ] }

  • Please refer to these guidelines to resolve the issue. Please inform me if this resolves it.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions