Sam local template handler within sub directory - node.js

I have a file called index.js within a sub-folder called api. The template.yaml is in the root folder. I am trying to add the handler (index.js) to the template but get the following error -
Mounting /Users/......./services/api as /var/task:ro inside runtime container
START RequestId: 62e6c91c-8c27-167a-d34c-b2e7aab1d11e Version: $LATEST
Unable to import module 'index': 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/appLpVideoList.js:3:15)
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)
The template.yaml looks like -
VideoList:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs6.10
CodeUri: api/
Events:
EventDetail:
Type: Api
Properties:
Path: /videos
Method: get
Environment:
Variables:
NODE_ENV: local

So instead of using sam local I used serverless-offline and it solved my issue. U can find the details about serverless-offline here -
https://github.com/dherault/serverless-offline

Related

Cannot find module 'fs/promises' using JHipster

I'm using nvm and I'm having this error when using jhipster. It was previously working when I'm not using nvm.
jhipster version: v7.9.3
node: v12.22.12
npm: 6.14.16
ERROR! Cannot find module 'fs/promises'
Require stack:
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/generators/bootstrap/index.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/lib/util/esm.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/lib/store.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/lib/environment.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/environment-builder.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/program.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/cli.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/jhipster.js
Error: Cannot find module 'fs/promises'
Require stack:
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/generators/bootstrap/index.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/lib/util/esm.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/lib/store.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/lib/environment.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/environment-builder.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/program.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/cli.js
- /Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/jhipster.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/generators/bootstrap/index.js:29:18)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/generators/bootstrap/index.js',
'/Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/lib/util/esm.js',
'/Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/lib/store.js',
'/Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/lib/environment.js',
'/Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/environment-builder.js',
'/Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/program.js',
'/Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/cli.js',
'/Users/xxxx/.nvm/versions/node/v12.22.12/lib/node_modules/generator-jhipster/cli/jhipster.js'
]
}
Anyone have any idea why is the error and how to fix it? Thanks
I was able to make it work by using node 16.14.2

Electron: TypeError: Cannot create property 'default' on symbol 'Symbol(nodejs.util.inspect.custom)'

I've developed an electron app with following tech stack:
NodeJS (10.15) with Babel to allow ES6 import/export syntax.
Sequelize on SQLite and GraphQL to handle an internal database.
ReactJS (bundled) for views.
I've configured everything to work with Electron and running "electron" command in project root folder works without errors.
Now i'm trying to distribute it using this tutorial: https://electronjs.org/docs/tutorial/application-distribution
So i've simply downloaded the 4.1.4 binaries for windows and moved the project folder under resources/app/
But running the electron.exe i've the following error:
App threw an error during load
TypeError: Cannot create property 'default' on symbol 'Symbol(nodejs.util.inspect.custom)'
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\nodejsCustomInspectSymbol.js:18:84)
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\nodejsCustomInspectSymbol.js:20:3)
at Module._compile (internal/modules/cjs/loader.js:711:30)
at Module._compile (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:722:10)
at Object.newLoader [as .js] (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:559:12)
at Function.Module._load (internal/modules/cjs/loader.js:551:3)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\defineToJSON.js:8:57)
at Object.<anonymous> (C:\workspace\myapp\resources\app\node_modules\graphql\jsutils\defineToJSON.js:36:3)
at Module._compile (internal/modules/cjs/loader.js:711:30)
at Module._compile (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:722:10)
at Object.newLoader [as .js] (C:\workspace\myapp\resources\app\node_modules\pirates\lib\index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:559:12)
at Function.Module._load (internal/modules/cjs/loader.js:551:3)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:20:18)
i've also tried to run npx electron resources/app/ from binaries folder and i've got the same error.
So i think the issue is something about the paths.
i've also tried electron-packager and the error is the same because this tool does the same process of guide above.
What can i do to fix it?
UPDATE1: after some investigations i've found that babel causes the issues. I guess when you run a project from a parent folder. I'm trying to find a solution
i've solved using this kind of configuration for babel/register:
const rootPath=path.resolve(path.join(__dirname, "../../"));
require("#babel/register")({
root: rootPath,
extends: path.join(rootPath, '.babelrc'),
ignore: [/node_modules/],
only: [rootPath],
})

Including node modules in AWS Lambda

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?

Unable to import module 'index': Error in aws cloud watch logs while using claudia.js

Unable to import module 'index': Error in aws cloud watch logs.
upload lambda using claudia works fine but api does not work and cloud watch logs displays following errors:
Unable to import module 'index': 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/node_modules/request/request.js:12:21)
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 have some code that only works in local node version (so it passes validation locally), but fails once it goes to server version. use nvm to run node locally for lambda projects and you'll catch those errors before deployment
so running : "npm install npm" in project directory solves the issue. then uploaded that again to aws using claudia.
all works fine.
thanks Gojko Adzic.

Webpack.config alias resolves during build but not during page load

I'm new to webpack and trying to work with some of the scaffoldings out there.
I'm trying to add an alias to the webpack config.
resolve: {
alias: {
'_utils': path.join(PATH_SRC, '/utils'),
},
}
It resolves fine during the build:
./src/utils/debounce.js
But the alias path doesn't get resolved when I try to load the page in the browser.
I feel like I must be missing something basic... Do I maybe need to pass these settings through to the hot-loader or something?
Here's the error I get
GET / 500 238.910 ms - 882
Error: Cannot find module '_utils/debounce'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:289:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (/.../src/www/routes/RootRoute/CodeRoute/Component.js:23:17)
at Module._compile (module.js:425:26)
at loader (/.../node_modules/babel-register/lib/node.js:127:5)
at Object.require.extensions.(anonymous function) [as .js] (/.../node_modules/babel-register/lib/node.js:137:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
There is a demo repo here:
https://github.com/erchaves/example-app/tree/config-resolve-alias-example
(use npm run dev)
With notes in this commit:
https://github.com/erchaves/example-app/commit/3486b230a3188df19fcdcbb8f9401d2b4b8400e9
Thanks!

Resources