Camaro : Module not found: Can't resolve xxxx - node.js

using Camaro npm package to transform XML in Server-less framework lambda project and getting error when running sls offline command -
Module not found for asm2wasm, env, global
WebAssembly module is included in initial chunk.
This is not allowed, because WebAssembly download and compilation must happen asynchronous.
Add an async splitpoint (i. e. import()) somewhere between your entrypoint and the WebAssembly module:

Related

Using ES modules in AWS Lambda created by Amplify CLI

According to the AWS blog, use of ES modules in Lambda is supported as of the Nodejs14 runtime.
Announcement - https://aws.amazon.com/about-aws/whats-new/2022/01/aws-lambda-es-modules-top-level-await-node-js-14/
Example - https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/
I have checked that the Lambda function runtime is Node14 (and tried switching to Node18 without any difference) - I checked via the Lambda console once I'd pushed the code, and checked that the version changes to 18 when the setting in the Amplify config is changed.
I won't go into the detail of how I got here, other than I need to use an npm package that is written to ESM syntax.
As a sanity check and as a minimum reproducable example, I generated a new simple hello world function with the Amplify CLI, and then ran it with amplify mock function test --event src/event.json and confirmed it runs ok. But when I change the package.json to "type":"module" I get:
stack: 'Error: Could not load lambda handler function due to Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /[redacted]/amplify/backend/function/test/src/index.js\n' +
'require() of ES modules is not supported.\n' +
'require() of /[redacted]/amplify/backend/function/test/src/index.js from /snapshot/repo/build/node_modules/amplify-nodejs-function-runtime-provider/lib/utils/execute.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.\n'
I get this same error whether I exercise the function from the amplify mock function CLI, the Lambda console, or by accessing the API gateway that links to the Lambda function.
Beyond the link blog posts above, I can't find any other mention or examples of using ES Modules with Lambda.
If you want to do this yourself:
Install amplify cli
amplify init
amplify add function and name it test, choose Nodejs, Hello World template
amplify mock function test --event src/event.json and it will work
Change amplify/backend/function/test/src/package.jsonto include"type":"module"`
amplify mock function test --event src/event.json and it will fail
Optionally you can push the application to AWS and test the lambda through the lambda console, you should get the same results.
Related issues:
https://github.com/aws-amplify/amplify-cli/issues/10437: Same issue in that the module being imported is ESM, and resolve in that a version update to the module provided CJS support
https://github.com/aws-amplify/amplify-cli/issues/5691: This relates to the root project being ESM (type: module in package.json) and monkey patching the package.json via amplify hooks, but doesn't address the issue of importing an ESM package. There is a comment at the bottom that claims its fixed in amplify CLI 10.2.3, but might have only addresses the root package issue, and not the lambda sub-project where I've encountering it.
https://github.com/aws-amplify/amplify-cli/issues/10432: Relates to use of 'mock function', but the problem relates to mocked or deployed functions. The steps outlined are about continuing to generate CJS output from typescript, converting the TS codes ESM style import/export. Because the code that amplify runs is CJS, if you try to import an ESM package it'll fail.

How to import from a bundle created in webpack?

I'm working on a project that's based on a given sample project that uses a package named Seeso.
The sample project uses the 'cross-env' and 'parcel-bundler' packages which are both deprecated, and I would like to replace them with pure node.
I managed to create a backend that statically serves requested files and thus handles imports,
but the Seeso package seems to dynamically resolve its imports using a webpack bundle (I have little to no knowledge of webpack but not even a configuration file is present; just the bundle), and I get the following error after I changed the import path of Seeso in the easy-seeso.js file to its actual path:
Uncaught SyntaxError: The requested module '/external_modules/seeso/dist/seeso.js' does not provide an export named 'CalibrationAccuracyCriteria' (at easy-seeso.js:2:34)
because of
import Seeso, {InitializationErrorType, CalibrationAccuracyCriteria} from '/external_modules/seeso/dist/seeso.js';
How can I import the needed Seeso files from the webpack bundle with minimum work and understanding of webpack as possible? (Preferably without running webpack commands before running - would like to run 'node server.js' and that's it)
Here is the sample project:
https://github.com/visualcamp/seeso-sample-web

Flutter In_App_Purchase Error: Error [ERR_REQUIRE_ESM]: change require() of ES Module \node_modules\camelcase-keys\index.js to dynamic import()

I am trying to setup Flutter In_App_Purchase (The offical Flutter In_App_Purchase Package) and I run into a problem with the firebase-backend. Whenever I try to deploy it, I run into the following error:
Failed to load function definition from source: Failed to generate manifest from function source: Error
[ERR_REQUIRE_ESM]: require() of ES Module C:\Users\larak\Desktop\Fashionbook\FashionLookbook_FlutterIAP_Backend-IAP\firebase-backend\functions\node_modules\camelcase-keys\index.js from C:\Users\larak\Desktop\Fashionbook\FashionLookbook_FlutterIAP_Backend-IAP\firebase-backend\functions\lib\app-store.purchase-handler.js not supported.
Instead change the require of index.js in C:\Users\larak\Desktop\Fashionbook\FashionLookbook_FlutterIAP_Backend-IAP\firebase-backend\functions\lib\app-store.purchase-handler.js to a dynamic import() which is available in all CommonJS modules.
I think it's related to this line in app-store.purchase-handler.ts:
import camelCaseKeys from "firebase-backend/functions/scr/camelcase-keys";
Full example code here: https://github.com/flutter/codelabs/tree/master/in_app_purchases/complete/firebase-backend
I tried to change it as described in the error message, but I wasn't able to achieve it. I have zero experience with TypeScript. I'm a Flutter developer and I usually use Node.js with JavaScript.

How do I use zlib in a react app? or what is zlib_binding and why is it missing?

I have created a brand new react app using VS2022 and create-react-app.
I have installed zlib using npm install zlib.
getting the following error when running npm run start:
ERROR in ./node_modules/zlib/lib/zlib.js 1:0-43
Module not found: Error: Can't resolve './zlib_bindings' in 'c:\projects\mall\mall\node_modules\zlib\lib'
To troubleshoot, I created a test.js file with just require('zlib') in it and it works.
file ./node_modules/zlib/lib/zlib.js has import './zlib_bindings' as a single line in it. But there is no ./zlib_bindings.js.
Questions:
How do I fix this?
How does that work when I run node test.js?
Your test.js file works because you run it with node.
The zlib module provides compression functionality implemented using
Gzip and Deflate/Inflate. It is the part of nodejs core module written
in c++
This module can't be used outside of node.js
You can however try to use react-zlib-js and it should work!

Unable to import native nodejs module in electron project

I'm working on an open source electron project which I am building using webpack. One requirement for my project is to use the nodegit library which has to be built as a native module.
I've followed what appears to be conventional advice when working with native modules and electron. That is, I run electron-rebuild, have configured the source package to use and finally have configured node-loader to catch the import of any .node files.
Unfortunately, when I go to include the module, I end up with this error:
ERROR in ./node_modules/nodegit/dist/nodegit.js
Module not found: Error: Can't resolve '../build/Debug/nodegit.node' in
'C:\Users\atrauzzi\Development\atrauzzi\gerty\node_modules\nodegit\dist'
# ./node_modules/nodegit/dist/nodegit.js 18:11-49
# ./src/Layer/Domain/Thunktor/Git/CloneGitRepository.ts
# ./src/Layer/Gerty/Component/Repository/AddGitHubRepository.tsx
# ./src/Layer/Gerty/Component/Repository/AddRepository.tsx
# ./src/Layer/Gerty/Component/Workspace.tsx
# ./src/Layer/Gerty/Component/App.tsx
# ./src/Layer/Gerty/GertyServiceProvider.ts
# ./src/Bundle/GertyElectron.ts
The only thing I can see that's suspicious at this point is that when I rebuild the module to work with electron, I only get a Release directory, when the import seems to be looking for Debug:
This could be a red herring however as nodegit is written to try Debug as a fallback after Release has failed.
The general ask here is "How do I get this native module working in my project?".
I also have a corresponding question over at the repo, although on the off chance that my issue is unrelated to the library itself, or that there are some battle-worn veterans of native modules in electron, I figured SO would be a good place to check as well.
Try configuring your webpack by specifying the native module as an external dependancy rather than load it using the node-loader.
https://webpack.js.org/configuration/externals/

Resources