code build to create lambda layer - cannot find module - aws-lambda

I want package a lambda layer using codebuild.
My codebuild buildspec is as follows:
version: 0.2
phases:
install:
runtime-versions:
nodejs: 10
commands:
- npm init -y
- npm install --save middy
artifacts:
files:
- 'node_modules/**/*'
- 'package-lock.json'
- 'package.json'
This saves a nodejs.zip folder to my s3 bucket
the resulting zip file looks like this:
package.json is as follows:
{
"name": "src",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"middy": "^0.30.4"
}
}
however when I add this layer to my lambda (node10.x)
and import my mods:
'use strict';
var AWS = require('aws-sdk');
const middy = require('middy')
const { cors } = require('middy/middlewares')
Returns the following error:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'middy'\nRequire stack:\n- /var/task/function_code/verify_zipcode.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'middy'",
"Require stack:",
"- /var/task/function_code/verify_zipcode.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" 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:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:956:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)",
" at Module.load (internal/modules/cjs/loader.js:812:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:724:14)",
" at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)",
" at internal/main/run_main_module.js:17:11"
]
}
Adding the envoronment variable:
NODE_PATH : ./:/opt/node_modules gave my lambda access to my layers, but lost the context of aws-sdk
After adding the env var I get the following error:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/function_code/verify_zipcode.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'",
"Require stack:",
"- /var/task/function_code/verify_zipcode.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" 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:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:956:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)",
" at Module.load (internal/modules/cjs/loader.js:812:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:724:14)",
" at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)",
" at internal/main/run_main_module.js:17:11"
]
}
Is there a way to use both the native aws-sdk and my layers? Or do I need to use an aws-sdk layer anytime I use other custom layers?

The directory structure of a node.js layer (nodejs.zip in your example) should be:
├── nodejs
└── package.json
└── node_modules
└── middy(version z.z.z)
Update your buildspec file to add a parent nodejs folder:
version: 0.2
phases:
install:
runtime-versions:
nodejs: 10
commands:
- mkdir nodejs # NEW LINE
- cd nodejs # NEW LINE
- npm init -y
- npm install bcrypt
artifacts:
files:
- 'nodejs/**/*' # CHANGE LINE
- 'package.json'
and remove the environment variable:
NODE_PATH

Related

Error: Cannot find module 'node-xlsx' while running cypress jenkins integration

The error below showing after executing in jenkins:
`Your configFile threw an error from: /var/lib/jenkins/workspace/PluginsUI/cypress.config.js
The error was thrown while executing your e2e.setupNodeEvents() function:
Error: Cannot find module 'node-xlsx'
Require stack:
/var/lib/jenkins/workspace/PluginsUI/cypress/plugins/index.js
/var/lib/jenkins/workspace/PluginsUI/cypress.config.js
/var/lib/jenkins/.cache/Cypress/12.4.1/Cypress/resources/app/node_modules/#packages/server/lib/plugins/child/run_require_async_child.js
/var/lib/jenkins/.cache/Cypress/12.4.1/Cypress/resources/app/node_modules/#packages/server/lib/plugins/child/require_async_child.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
at Function.Module._load (node:internal/modules/cjs/loader:804:27)
at Module.require (node:internal/modules/cjs/loader:1028:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (/var/lib/jenkins/workspace/PluginsUI/cypress/plugins/index.js:26:16)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Module.require (node:internal/modules/cjs/loader:1028:19)
at require (node:internal/modules/cjs/helpers:102:18)
at setupNodeEvents (/var/lib/jenkins/workspace/PluginsUI/cypress.config.js:11:14)
at /var/lib/jenkins/.cache/Cypress/12.4.1/Cypress/resources/app/node_modules/#packages/server/lib/plugins/child/run_plugins.js:118:14
at tryCatcher (/var/lib/jenkins/.cache/Cypress/12.4.1/Cypress/resources/app/node_modules/bluebird/js/release/util.js:16:23)
at Function.Promise.attempt.Promise.try (/var/lib/jenkins/.cache/Cypress/12.4.1/Cypress/resources/app/node_modules/bluebird/js/release/method.js:39:29)
at RunPlugins.load (/var/lib/jenkins/.cache/Cypress/12.4.1/Cypress/resources/app/node_modules/#packages/server/lib/plugins/child/run_plugins.js:115:9)
at RunPlugins.runSetupNodeEvents (/var/lib/jenkins/.cache/Cypress/12.4.1/Cypress/resources/app/node_modules/#packages/server/lib/plugins/child/run_plugins.js:276:10)
at EventEmitter. (/var/lib/jenkins/.cache/Cypress/12.4.1/Cypress/resources/app/node_modules/#packages/server/lib/plugins/child/run_require_async_child.js:185:22)
at EventEmitter.emit (node:events:513:28)
at process. (/var/lib/jenkins/.cache/Cypress/12.4.1/Cypress/resources/app/node_modules/#packages/server/lib/plugins/util.js:33:22)
at process.emit (node:events:513:28)
at emit (node:internal/child_process:939:14)`
I'm trying to execute the test case in cypress on jenkins pipelin configuration:
pipeline{
agent any
parameters{
string(name: 'SPEC', defaultValue: "cypress/e2e/1.Gateway/MqttGateway.cy.js", description: "Enter the script path that you want to execute")
choice(name: 'BROWSER', choices: ['chrome', 'edge', 'firefox'], description: "Choose the browser where you want to execute your scripts")
}
options{
ansiColor('xterm')
}
stages{
stage('Building'){
steps{
echo "Building the application"
}
}
stage('Testing'){
steps{
sh "npm i"
sh "npx cypress run --browser ${BROWSER} --spec ${SPEC}"
}
}
stage('Deploying'){
steps{
echo "Deploy the application"
}
}
}
post{
always{
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: true, reportDir: 'cypress/report', reportFiles: 'index.html', reportName: 'HTML Report', reportTitles: '', useWrapperFileDirectly: true])
}
}
}

Package subpath './index.cjs' is not defined by \"exports\" in /var/task/node_modules/nanoid/package.json in AWS Lambda (nanoid)

My NodeJS Lambda is failing with this error while trying to use nanoid or uuid:
{
"error": "Error",
"cause": {
"errorType": "Error",
"errorMessage": "Package subpath './index.cjs' is not defined by \"exports\" in /var/task/node_modules/nanoid/package.json",
"trace": [
"Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './index.cjs' is not defined by \"exports\" in /var/task/node_modules/nanoid/package.json",
" at throwExportsNotFound (internal/modules/esm/resolve.js:290:9)",
" at packageExportsResolve (internal/modules/esm/resolve.js:513:3)",
" at resolveExports (internal/modules/cjs/loader.js:437:36)",
" at Function.Module._findPath (internal/modules/cjs/loader.js:477:31)",
" at Function.Module._resolveFilename (internal/modules/cjs/loader.js:872:27)",
" at Function.Module._load (internal/modules/cjs/loader.js:730:27)",
" at Module.require (internal/modules/cjs/loader.js:957:19)",
" at require (internal/modules/cjs/helpers.js:88:18)",
" at /var/task/__index.js:39:45",
" at Object.__f4 [as getPaths] (/var/task/__index.js:58:6)"
]
}
}
Here is the code used:
import { nanoid } from 'nanoid'
const id = nanoid()
Other information:
Runtime: nodejs14.x
nanoid: ^3.1.23

Cannot find module 'handler' when running simple lambda deployed by serverless

I am trying to start learning serverless / lambda so I created a simple lambda and deployed it with serverless which worked.
However when I want to test the endpoint of the lampbda I get a 502 back. When I look in the logs it tells me that it can not find the module handler which does not make any sense...
here is the log:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'handler'\nRequire stack:\n-
/var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'handler'",
"Require stack:",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" 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:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1158:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)",
" at Module.load (internal/modules/cjs/loader.js:1002:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:901:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)",
" at internal/main/run_main_module.js:18:47"
]
}
This normally means that it can not find the method that is the starting point to execute.
For example on your serverless.yml you can have something like this
functions:
getUsers:
handler: userFile.handler
this would mean that it's required to have a userFile in the same folder of the serverless.yml with the method handler exported.
module.exports.hello = async event => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
},
null,
2
),
};
};
Note that it does not need to be named handler function, just needs to have the same name defined on the serverless.yml
I ran into the same error when launching a lambda locally using AWS sam, with Webstorm.
Turns out a previous run had not correctly stopped and destroyed the docker container running the lambda. Stopping and removing said docker container fixed the problem for me.

Webpack SASS loader with alias not compiling under Mocha

I have a component that loads styles like this. The css directory is somewhere else; here it is being used as a Webpack alias.
import 'css/components/PromptText';
// ...
class PromptText extends React.Component {
// ...
}
Here is my webpack.config.json:
var path = require('path');
module.exports = {
entry: './src/index.jsx',
output: {
path: 'dist',
filename: 'app.bundle.js',
},
module: {
loaders: [{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
}, {
test: /\.json/,
exclude: /node_modules/,
loader: 'json',
}, {
test: /\.scss/,
exclude: /node_modules/,
loaders: ['style', 'css', 'sass'],
}],
},
resolve: {
alias: {
css: 'css', // <-- Alias here
},
root: path.resolve(__dirname),
extensions: ['', '.js', '.jsx', '.scss'],
},
};
Now, I have a test like this for Mocha:
import PromptText from '../../src/components/PromptText';
describe('PromptText', () => {
it('should display words');
});
When Mocha imports the component, it also tries to load the CSS component. There are two problems with this:
The css directory has been aliased. Mocha needs to know where the alias points. To fix this, I have installed the babel-plugin-webpack-alias package, with the following .babelrc:
{
"presets": ["es2017", "react", "stage-0"],
"env": {
"test": {
"plugins": [
["webpack-alias", {"config": "webpack.config.js"}]
]
}
}
}
CSS can't be imported by Mocha, so it needs to be ignored. I use the ignore-styles package and invoke Mocha as mocha --compilers js:babel-core/register --require ignore-styles.
Despite having done these two things, I get the following error when trying to run tests:
(cd data && make)
make[1]: Nothing to be done for `all'.
NODE_ENV=test ./node_modules/.bin/mocha --compilers js:babel-core/register --require ignore-styles \
$(find test -type f -name 'test*.js')
module.js:341
throw err;
^
Error: Cannot find module 'css'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/waleed/Workspace/js/steno/src/components/PromptText.jsx:2:1)
at Module._compile (module.js:413:34)
at loader (/Users/waleed/Workspace/js/steno/node_modules/babel-register/lib/node.js:146:5)
at Object.require.extensions.(anonymous function) [as .jsx] (/Users/waleed/Workspace/js/steno/node_modules/babel-register/lib/node.js:156:7)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/waleed/Workspace/js/steno/test/components/testPromptText.js:2:1)
at Module._compile (module.js:413:34)
at loader (/Users/waleed/Workspace/js/steno/node_modules/babel-register/lib/node.js:146:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/waleed/Workspace/js/steno/node_modules/babel-register/lib/node.js:156:7)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:20:19)
at /Users/waleed/Workspace/js/steno/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/Users/waleed/Workspace/js/steno/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/Users/waleed/Workspace/js/steno/node_modules/mocha/lib/mocha.js:485:10)
at Object.<anonymous> (/Users/waleed/Workspace/js/steno/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:148:18)
at node.js:405:3
make: *** [test] Error 1
For some reason, the css alias isn't being resolved. However, this error only happens sometimes, unpredictably. In order to consistently recreate this error, I have to set BABEL_DISABLE_CACHE=1 in the environment.
How can I get my Mocha test to correctly import and ignore the CSS file?
In order to fix this, I switched from the babel-plugin-webpack-alias to the babel-plugin-webpack-aliases package, and update my .babelrc to use it:
{
"presets": ["es2017", "react", "stage-0"],
"env": {
"test": {
"plugins": [
["webpack-aliases", {"config": "webpack.config.js"}]
]
}
}
}
I also always run mocha with BABEL_DISABLE_CACHE=1 set in the environment. (I think this is undoing the damage that babel-plugin-webpack-alias did; it may not be necessary on every run if that package was never used.)

How can I configure a 'hello world' example of nightwatchjs working in circleci?

I'm trying to set up a sample circleci build using docker-compose. Everything is working fine but when I try to add a nightwatchjs test, something is wrong with the nightwatch bootstrapping/install. No matter what I do I can't seem to be able to get nightwatch to fire off.
This is the error I get in circleci;
nightwatch -t /home/ubuntu/docker-compose/tests/nightwatch.js
/home/ubuntu/docker-compose/tests/nightwatch.js:2
"test_settings": {
^
SyntaxError: Unexpected token :
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at new Module (/home/ubuntu/nvm/v0.10.33/lib/node_modules/nightwatch/lib/runner/module.js:7:23)
at new TestSuite (/home/ubuntu/nvm/v0.10.33/lib/node_modules/nightwatch/lib/runner/testsuite.js:17:17)
at runTestModule (/home/ubuntu/nvm/v0.10.33/lib/node_modules/nightwatch/lib/runner/run.js:38:19)
at runNextModule (/home/ubuntu/nvm/v0.10.33/lib/node_modules/nightwatch/lib/runner/run.js:190:23) nightwatch -t /home/ubuntu/docker-compose/tests/nightwatch.js returned exit code 1
This is my circle file.
# circle.yml
machine:
services:
- docker
general:
branches:
only:
- master # list of branches to build
- develop
dependencies:
override:
- sudo pip install --upgrade docker-compose
- sudo pip install --upgrade pep8
- docker-compose build
- wget http://selenium-release.storage.googleapis.com/2.44/selenium-server-standalone-2.44.0.jar
- java -jar selenium-server-standalone-2.44.0.jar:
background: true
- npm install -g nightwatch
test:
override:
- docker-compose up -d; sleep 10
- pep8 --first tests/test.py
- nightwatch -t /home/ubuntu/docker-compose/tests/nightwatch.js
deployment:
hub:
branch: master
commands:
- chmod +x ./build/build-docker-dev.sh
- sudo ./build/build-docker-dev.sh
- docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
- docker push xxx/dockercompose_odoo
Here is my nightwatch.js file;
{
"test_settings": {
"default": {
"launch_url": "http://localhost",
"selenium_port": 4444,
"selenium_host": "localhost",
"silent": true,
"firefox_profile": false,
"chrome_driver": "",
"ie_driver": "",
"screenshots": {
"enabled": false,
"path": ""
},
"desiredCapabilities": {
"browserName": "firefox",
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
}
}
I guess this is too old but I suggest you to try updating the node version
# circle.yml
machine:
node:
version: 5.8.0

Resources