I am trying to create a nodejs lambda function with the web3 module.
However, when zipping my handler.js and the entire node_modules directory and testing the lambda, the function cannot find web3.
Structure of the zip file:
handler.js
node_modules/
-web3_module_1/
-web3_module_2/
-other_modules_used_by_web3/
According to this source, the zip file structure should be the following:
handler.js
web3_module_1/
web3_module_2/
other_module/
But even with this structure, I get an error:
Unable to import module 'handler': Error
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/handler.js:3:14)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
What is the correct way to achieve this?
Related
I have setup mocha to test typescript source files. I have successfully run a test on one file that only imported source files, but I'm seeing a failure running a test that imports a node module.
I am running this on the command line to run the tests:
mocha -r ts-node/register -r jsdom-global/register spec/**/**.spec.ts
In the test that is failing, the source file that it is testing has this import
import * as angular from "angular";
And when the test is run, I see this error:
ReferenceError: angular is not defined
at Object.<anonymous> (/Users/bsacamano/testRepos/myApp/node_modules/angular/index.js:2:18)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/bsacamano/testRepos/myApp/web/common/default/unsecured/shared-modules/services/flexlabel/flex.service.ts:1:1)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Module.m._compile (/Users/bsacamano/testRepos/myApp/node_modules/ts-node/src/index.ts:1455:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Object.require.extensions.<computed> [as .ts] (/Users/bsacamano/testRepos/myApp/node_modules/ts-node/src/index.ts:1458:12)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/bsacamano/testRepos/myApp/spec/shared-modules/services/flexlabel/flex.service.spec.ts:1:1)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Module.m._compile (/Users/bsacamano/testRepos/myApp/node_modules/ts-node/src/index.ts:1455:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Object.require.extensions.<computed> [as .ts] (/Users/bsacamano/testRepos/myApp/node_modules/ts-node/src/index.ts:1458:12)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.exports.requireOrImport (/Users/bsacamano/testRepos/myApp/node_modules/mocha/lib/nodejs/esm-utils.js:49:16)
at async Object.exports.loadFilesAsync (/Users/bsacamano/testRepos/myApp/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
at async singleRun (/Users/bsacamano/testRepos/myApp/node_modules/mocha/lib/cli/run-helpers.js:125:3)
at async Object.exports.handler (/Users/bsacamano/testRepos/myApp/node_modules/mocha/lib/cli/run.js:370:5)
I have a different test for a source file that has these imports
import {IHttpService, IPromise, IRequestConfig} from 'angular';
and that test completes without error.
Also, the code works in production; I'm seeing that angular is defined and working.
Any ideas on what I can do to get the test to pass?
Update:
I think it has to do with the clown show that is bare imports. My web app is built with webpack, and webpack takes care of converting those imports when bundling.
Out of desperation, I tried building a bundle for my tests (which I really didn't want to do since I'm not distributing the file, but you know). I then ran mocha on the bundle, but I'm still getting the same error, angular is not defined, just with a different stack trace.
Update 2:
Even if I were to remove the "bare" import and replace with an import of the script relative to the file, it doesn't work. I don't get the ReferenceError, but the import just doesn't happen; if I console.log(angular); after importing it, I see that it is an empty object.
I'm tired of this error! This is Serverless app with Lambda in AWS. Locally it works perfectly. Testing locally didn't work for load of other reasons. I'm testing in staging. Yet I'm getting this error:
Unable to import module 'src/engine/handler': Error
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous>(/var/task/src/engine/controllers/image.js:43:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
I followed all the steps advised elsewhere including: moving all dependencies to dependencies (out of dev), installing serverless and all. I'm using Node 6.10.3 and TypeScript 3.1. Can you help?
If you're trying host your nodejs projects which has so many dependencies not related to aws services inside, you better zip your projects in S3 first then import lambda from S3.
I am using AWS Lambda for creating resized thumbnails. The source is an S3 bucket and the destination is another S3 bucket. I was able to create the lambda function from Amazon CLI. The function is triggered whenever a new image is added to the source bucket. But, the resize/thumbnail creation is not working. I can see some issue in the CloudWatch Console.
The content of the uploaded ZIP package is:
CreateThumbnail.js, node_modules
Unable to import module 'CreateThumbnail': Error
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/CreateThumbnail.js:2:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
Best regards,
BP
Finally able to find the source of the bug. The command used to make the zip file was causing the issue.
The correct command is:
zip -r CreateThumbnail.zip *
With the package.json, it's possible to do something like this
"dependencies": {
"WindowManager": "./lib/WindowManager"
},
But when trying to do this in Electron, it gives me this error:
App threw an error during load
Error: Cannot find module 'WindowManager'
at Module._resolveFilename (module.js:470:15)
at Function.Module._resolveFilename (.../desktop/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (.../desktop/lib/init.js:15:35)
at Object.<anonymous> (.../desktop/lib/init.js:22:3)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
Is it because of Electron, or am I doing something wrong?
./lib/WindowManager is a folder with a package.json in it with a main pointing to the WindowManager.js, so that should work.
The case you try to do normally works, make sure that the dependency "WindowManager" has been properly copied by the build tools to either:
.../desktop/node_modules/electron/dist/Electron.app/Contents/Resources/app.asar/node_modules
or
.../desktop/node_modules/electron/dist/Electron.app/Contents/Resources/node_modules
If WindowManager does not exist in the disted node_modules there is something wrong with your build process, hard to be more precise than that.
I'm trying to run a node.js script from my node.js server. The problem is that the modules I import with require () do not work anymore while all the modules and files are in the same folder.
var mysql = require('mysql');
^
ReferenceError: require is not defined
at evalmachine.<anonymous>:3:13
at ContextifyScript.Script.runInThisContext (vm.js:25:33)
at Object.runInThisContext (vm.js:97:38)
at Object.<anonymous> (D:\wamp\www\node_server\test_server.js:8:14)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
Thank you in advance,
I had a similar problem - server-side nodejs thinking that require is not defined.
It turns out the line "type": "module" in my package.json was causing this error, and once I removed that require worked as expected.