Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESM - ECMAScript Modules with JavaScript mock data #6

Closed
danactive opened this issue Aug 7, 2023 · 5 comments · Fixed by #7
Closed

ESM - ECMAScript Modules with JavaScript mock data #6

danactive opened this issue Aug 7, 2023 · 5 comments · Fixed by #7
Assignees
Labels
enhancement New feature or request

Comments

@danactive
Copy link

danactive commented Aug 7, 2023

Describe the issue
Node.js cannot be configured to ECMAScript modules (ESM) with a JavaScript mock data file.

JavaScript mock data must use CommonJS (CJS) export syntax module.exports with Node.js configured to support CJS (in package.json type must be omitted or set to "commonjs")

If Node.js is configured to ESM then the JavaScript mock data hits an exception on this line 75

Steps to reproduce
Steps to reproduce the behaviour:

  1. Update packge.json to "type": "module",
  2. Touch a file in the root with filename sandbox-invoke-mocks.js
  3. Define a mock structure with module.exports as .CJS or export default syntax .JS or .MJS

Expected behaviour
Execute lambda function with mock data

Actual behaviour

An empty mock event is passed to the Lambda function if the JavaScript mock file is a .CJS

ESM error if the JavaScript mock file is a .JS or .MJS

/Users/.../node_modules/@architect/plugin-lambda-invoker/src/index.js:75
            mocks = require(jsMocks)
                    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/.../sandbox-invoke-mocks.js from /Users/.../node_modules/@architect/plugin-lambda-invoker/src/index.js not supported.

Screenshots
If applicable, add screenshots to help explain your problem

Screenshot 2023-08-07 at 13 07 31

Environment
Please complete the following information (if appropriate):

  • OS: macOS Ventura 13.4 (with Apple Silicon chip)
  • Node.js: v18 (LTS)

Workaround
Node.js may be configured as ESM then use a JSON file for your mock data (to avoid the import/require conflict)!

@ryanblock
Copy link
Member

Good call, we can and should fix this!

@ryanblock ryanblock self-assigned this Aug 8, 2023
@ryanblock ryanblock added the enhancement New feature or request label Aug 8, 2023
@ryanblock ryanblock linked a pull request Aug 8, 2023 that will close this issue
10 tasks
@ryanblock
Copy link
Member

ryanblock commented Aug 8, 2023

Just released 1.1.0 RC, please give it a try with npm i @architect/plugin-lambda-invoker@RC --save-dev.

Please note this caveat as found in the changelog: per Node.js #2806, ES module data may be cached in Sandbox, which is intended to be a semi-long-lived process. If using ES modules, you may need to restart Sandbox when updating your mocks.

@danactive
Copy link
Author

Node.js configured as ESM using .JS or .MJS for my JavaScript mock file uses export default hits an exception

Node.js configured as ESM using .CJS for my JavaScript mock file uses module.exports hits the same exception

The shared exception on this line 155.

Lambda init error: Cannot use import statement outside a module
Screenshot 2023-08-08 at 07 58 35

@ryanblock
Copy link
Member

Thanks, I'll take a peek this morning and figure out where the bug is!

@danactive
Copy link
Author

danactive commented Aug 8, 2023

@ryanblock Correction, I tweaked my side and it works! RC.0 works for both .JS and .MJS with export default
and works with .CJS with module.exports. It also remains to work with JSON file extension! This issue is closed

ryanblock added a commit that referenced this issue Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

2 participants