AWS Lambda - Runtime.ImportModuleError: Error: Cannot find module 'jmespath' - node.js

I am working with aws lambda using serverless framework, I changed the runtime from nodejs8.10 to nodejs10.x, then I got an errortrace,
{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'jmespath'","stack":["Runtime.ImportModuleError: Error: Cannot find module 'jmespath'"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:36:30)"," at Module._compile (internal/modules/cjs/loader.js:701:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)"," at Module.load (internal/modules/cjs/loader.js:600:32)"," at tryModuleLoad (internal/modules/cjs/loader.js:539:12)"," at Function.Module._load (internal/modules/cjs/loader.js:531:3)"," at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)"," at startup (internal/bootstrap/node.js:283:19)"]}
What is the cause of this issue and how can get it fixed?

I hit the same problem Error: Cannot find module 'jmespath' and solved it.
Do you use aws-sdk via node_modules? like follows
var aws = require('aws-sdk');
If so you just remove aws-sdk from node_modules.
remove aws-sdk for yarn
yarn remove aws-sdk
remove aws-sdk for npm
npm uninstall aws-sdk
aws-sdk has been included to lambda since nodejs10.
see: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

Try to put ./ in front of your module name. I changed my name from require("xxx") to require("./xxx") and it worked again. In my case, the local module file i wanted to add (xxx.js) is on the same level as the index.js file.

I'd also like to add that as a preliminary step before adjusting your path, check and verify your dependencies listed in your package.json(s) files.
I've seen this error "Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'something' occur in the aws cloudwatch logs.
It happened because my project has multiple subprojects/subfolders with their own package.json files. Ensure that module is properly referenced in the subproject's package.json.
In local dev, you might have the dependency cited in your base/global package.json, and thinking it works -- but when you deploy the lambda the npm install that occurs during build does not include the newly required module because its not referenced in the local subproject's package.json.

In the root serverless project folder
npm i --save <npm module name missing>

Well encountered the same issue in our project too.
It was issue in the same of file import.
Our file name xabc.js and in imported as Xabc.js
VScode was not complaining and was showing proper import

For me the problem was that this jmespath library was actually missing(and some others, too) from uploaded node_modules folder.
Verified this by downloading zip(as the source was too big) and did not found there jmespath nor aws-sdk libraries.
Solved it by installing dependencies with npm i and re-uploading lambda with updated node_modules folder.

If 'nodejs' folder is not there in Lambda Layer, create 'nodejs' and any file so that while executing the function, lambda will create zip file with entire node_modules packages inside 'nodejs' folder of Layer. If it is empty it will not execute. I hope it will resolve the issue. In my case it is working fine.

Related

How to deploy Nuxt.js to Elastic Beanstalk?

I am very new to AWS and i have been following the tutorials out there but couldn't find the answer. So, what i did was i created simple nuxt application, no changes to the framework's script or anything. I set my elastic beanstalk to run node.js settings. and then i tried to deploy my /dist folder(using nuxt build) with the application's json folder as told by every tutorials. but it gives me this this is what it looks on my webpage
I think i have something wrong with which folders i deploy or do i have to actually deploy via the aws CLI, would that make a difference at all?
eb log :
-------------------------------------
/var/log/nodejs/nodejs.log
-------------------------------------
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /tmp/.npm/_logs/2019-09-29T16_00_28_939Z-debug.log
> portfolio#1.0.0 start /var/app/current
> cross-env NODE_ENV=production node server/index.js
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module '/var/app/current/server/index.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
The nodejs logs indicate to me that there is an issue with the build, an issue with how it's zipped, or an issue with your start command.
The most common is incorrectly zipping source files
When you create a ZIP file in Mac OS X Finder or Windows Explorer, make sure you zip the files and subfolders themselves, rather than zipping the parent folder.
In other words, do not right click the dist folder and click "zip", select all the files and folders (including hidden ones) and zip those. Elastic beanstalk, and many other AWS services, expect the zip to unpack into a flat structure, and not contain the parent folder when unzipped.
If this does not solve your problem, make sure you're zipping the correct folder. Try deleting your .nuxt and /dist folders, and running npm run build locally. The folder that is produced (defaults to .nuxt) is the folder whose contents need to be zipped.
The reason it is throwing that error is because your launch command starts with cross-env. When it sees that it tries to run the globally installed package cross-env but it is not available globally, so it throws that error. There are a few possible ways to address this:
The easiest is to just change the start command to use a relative path to the locally installed version of cross-env, so your command should look something like this:
node_modules/cross-env/src/bin/cross-env.js node server/index.js
You might also try adding npx to the front of your command, like so:
npx cross-env NODE_ENV=production node server/index.js
This should work but I have found that sometimes I get errors with specific packages, from what I can tell it is a bug with npx but I haven't had time to look into it. Notably I did see this bug with a nuxt js app specifically so you might too, however I was using the nuxt cli which you are not.
The third option is you could look into installing the package globally using ebextensions. In my work I haven't come across a situation where that has been necessary.

Cannot find module app.js?

I am starting node app.js in the server directory, and I get the following:
module.js:544
throw err;
^
Error: Cannot find module 'server/server/app.js'
at Function.Module._resolveFilename (module.js:542:15)
at Function.Module._load (module.js:472:25)
at Function.Module.runMain (module.js:682:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:613:3
[nodemon] app crashed - waiting for file changes before starting...
How to fix this? It was working before, and now it's looking for a weird path /server/server instead of just /server.
I just reinstalled node to a more up-to-date version, from 5. to latest stable.
Most likely you try to load your module like so:
require('server/server/app.js')
Node.js then tries to find it somewhere below node_modules. But I suppose, the file is not a dependency but part of your own code. Normally, you would then require it like so (consider the point at the beginning):
require('./server/server/app.js')
This makes Node.js look for the file beginning at the current directory ('.').
This would work in an environment with the following folder structure:
/myfiles/main.js <-- this file contains the `require` statement
/myfiles/server/
/myfiles/server/server/
/myfiles/server/server/app.js <-- this file is being `require`d
I encountered with this issue because I deleted build folder and then try to build the project.
Solution - in my case
I run the tsc --watch that compiled all .ts files to .js
maybe you want to access file app.js,
require('./server/server/app.js')
// to be like this
require('server/server/app.js')
use can using this package
npm i app-module-path

ElasticBeanstalk nodejs.log can't find module 'hogan.js'

I have a simple node.js app that is using "hogan": "^1.0.2" (from packages.json under "dependencies").
It has been failing to deploy, and looking in the logs, I am seeing (multiple times):
Error: Cannot find module 'hogan.js'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
I'm fairly new to node.js on EB, and have just been following http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs_express.html with a pre-existing Express app.
Assuming EB runs npm install for me (is that a safe assumption?), what might the issue be?
I was using "hogan": "^1.0.2" instead of "hogan.js": "^3.0.2", which worked locally but not on EB.
hogan is just an unofficial alias someone made to hogan.js.
I do not get hogan.js errors any more after making the switch.
For what it's worth, I also had an issue similar to this and determined that it was due to file case issues. In the node EB container, the OS is case-sensitive, and because I was ignoring file case changes in git, it didn't update the file casing in the repository.
You should run git config core.ignorecase false if this is the issue.
You might also change "hogan": "^3.0.2" to "hogan": "*" because EB does run npm install for you. If EB can't find the specific version with "^", it might not install the package as you did locally. Also you might check your file structure before you do eb create or eb deploy. You might include something unnecessary if you are using the command line. I suggest that you create an application.zip file with files you need, such as package.json, app.js and index.html and upload it through GUI. Don't ever pack up node_modules in your .zip file, that's troublesome.

How to load node modules in beaker notebook?

I'm using beaker notebook and would like to use some external node modules - yet because beaker doesn't change the system path to the directory of the currently active file, my attempts to include stream-filter are met with the error:
Error: Cannot find module 'stream-filter'
Error: Cannot find module 'stream-filter'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at eval (eval at processCode (/opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/app.js:45:23), :5:14)
at processCode (/opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/app.js:45:18)
at /opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/app.js:32:28
at callbacks (/opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/node_modules/express/lib/router/index.js:164:37)
at param (/opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/node_modules/express/lib/router/index.js:138:11)
at pass (/opt/homebrew-cask/Caskroom/beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/node_modules/express/lib/router/index.js:145:5)
I'm wondering how to solve this problem. I tried process.chdir('/Users/akivalipshitz/Developer/Computational_Linguistics') to no avail, even though stream-filter is installed in node_modules in the same directory.
So how do people use node modules
The context where the node code is eval'd is in your case
/opt/homebrew-cask/Caskroom//beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/app.js
So require() looks for a module in the node_modules folder there and then recursively up along the parent folders (see docs).
One solution (if you're only working on one project or use this module frequently across all your projects) would be to install your module there:
cd /opt/homebrew-cask/Caskroom//beaker/1.4.2-1-ge55c059/Beaker.app/Contents/Resources/dist/config/plugins/eval/node/app/
npm install stream-filter
Another (ugly) approach could be to build a long relative path in your require(). In your case something like:
require('../../../../../../../../../../../../Users/akivalipshitz/Developer/Computational_Linguistics');
Both cases it would mean that your code is not portable to another beaker instance. However, at least until a better solution is proposed this could get you along prototyping.
as of this writing, the current version of Beaker allows you to import npm modules right from the GUI. Just click notebook -> Language Manager -> Javascript, then type the module names into Loaded Libraries. Beaker autocompletes from the npm registry.
This works both in the electron client and the cloud hosted version.

"Error: Cannot find module 'less'" Node.js module loading preference/order/cache?

Here's the situation… So I've created a Node.js module that acts as an interface to some Node.js template engines, Shift.js. That is included inside another Node.js module, Design.io (it's specified Design.io's package.json dependencies block). Design.io watches files for changes.
Then you build an app and require('design.io'). You also install, say, Less and Stylus into your project.
This way, when you save a .less or .styl file, a chain of methods are called:
require("design.io") gets notified of the filesystem event. Design.io then calls
require('shift').render(string, extension, callback). Shift.js then calls
require(moduleFor(extension)) # require("less"). The string is compiled.
In my app (current working directory) I have installed less and stylus:
npm install less stylus
The problem I'm having is, in step 3 which is called from within the Shift.js module, I get errors like this:
Error: Cannot find module 'less'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Less.engine (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift/less.js:6:14)
at Less.render (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift/less.js:18:21)
at /Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift.js:69:23
at /Users/viatropos/Documents/git/plugins/design.io/node_modules/async/lib/async.js:118:13
at Object.forEachSeries (/Users/viatropos/Documents/git/plugins/design.io/node_modules/async/lib/async.js:134:9)
at Object.render (/Users/viatropos/Documents/git/plugins/design.io/node_modules/shift/lib/shift.js:78:31)
My question is, why is this happening? I thought that you could dynamically require libraries from a module as long as they were installed somewhere… What am I missing?
The goal would be that libraries like Shift.js wouldn't have to define every single dependency in package.json… For an "interface to the template engines" library, that would require too many dependencies that the app would probably never be using.
Thanks for the help, hope that was somewhat clear.
When you npm install foo, the foo module gets installed in a directory named node_modules in the current working directory.
When you install this shift library, it only looks for modules to require within its own node_modules directory, or in one of the global directories require.resolve() searches.
And that's the simple solution to your problem:
npm install -g less
And then the library is globally visible to all Node.js code on your computer, rather than only being visible to code in the current working directory.
Alternatively, if you only want shift to see it, then do something like this:
npm install shift
cd node_modules/shift
npm install less

Resources