Runtime.ImportModuleError - Error: Cannot find module 'tslib' - node.js

error message
undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'tslib'"}
folder structure
src
handler.ts -> fn.getAll
template.yaml
GetAll:
Type: AWS::Serverless::Function
Properties:
Handler: handler.getAll
CodeUri: dist/out-tsc
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref master
Environment:
Variables:
USE_EVENT_BRIDGE: !Ref UseEventBridge
Events:
Api1:
Type: Api
Properties:
Path: /getAll
Method: get
Output folder:
dist/out-tsc
handler.js -> fn.getAll
After typescript compile using tsc. output files are generated at targeted location. In each JS handler file i have tslib require statement. Below error message denotes the same. When I click on the tslib it refers to the node modules perfectly. How can I overcome this?
I have updated the tsconfig.json with below options in compileroptions. Installed tslib in node modules
"paths": {
"tslib": ["node_modules/tslib/tslib.d.ts"]
}
Error:
START RequestId: 820d8905-61f3-4045-9704-3cc7bd4901df Version: $LATEST
2021-02-12T11:18:32.904Z
undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'tslib'","stack":["Runtime.ImportModuleError: Error: Cannot find module 'tslib'","
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:45:30)","
at Module._compile (internal/modules/cjs/loader.js:778:30)","
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)","
at Module.load (internal/modules/cjs/loader.js:653:32)","
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)","
at Function.Module._load (internal/modules/cjs/loader.js:585:3)","
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)","
at startup (internal/bootstrap/node.js:283:19)"]}

Related

HandlerNotFound at trigger aws lambda function

I have a problem with deployed to AWS Lambda function. I create an trigger to SQS queue, when I have new message then my trigger (index.receiver) processing this message. So, here is my code:
index.ts
import receiver from "./sqs/receiver";
exports.receiver = receiver;
sqs/receiver.ts
const receiver: SQSHandler = async (event) => {
//some logic
};
export default receiver;
After trigger this function, I see in AWS CloudWatch that nothing is working correctly and throw me this error:
{
"errorType": "Runtime.HandlerNotFound",
"errorMessage": "index.receiver is undefined or not exported",
"stack": [
"Runtime.HandlerNotFound: index.receiver is undefined or not exported",
" at Object.module.exports.load (/var/runtime/UserFunction.js:144:11)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" 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 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
" at internal/main/run_main_module.js:17:47"
]
}
Here is also my project tree:
.serverless
node_modules
sqs
- receiver.ts
index.ts
package.json
serverless.yml
and serverless.yml
service: some-name
provider:
name: aws
runtime: nodejs12.x
region: eu-central-1
apiGateway:
minimumCompressionSize: 1024 # Enable gzip compression for responses > 1 KB
environment:
AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1
# SQS Permission given
iamManagedPolicies:
- 'arn:aws:iam::aws:policy/AmazonSQSFullAccess'
functions:
receiver:
handler: index.receiver
events:
- sqs: <my-sqs-arn>
Can someone tell me what I'm doing wrong? Thanks for any help!
I resolve this problem. Problem was in this, that in my serverless.yml I point handler like this: handler: index.receiver but, when we use Typescript we should point it to dist/index.receiver because AWS can handle only JS files, we also need to remember about build project with this function before using serverless deploy.

Imports from tsconfig paths not working with bazel: Cannot find module '#...'

Repo for reproduction: https://github.com/timothyaltemus/bazel-typescript-stackoverflow-example.
I am running into an issue where I am unable to use my tsconfig paths configuration for importing in my TypeScript files.
tsconfig.json:
{
...
"compilerOptions": {
"baseUrl": ".",
"paths" {
"#*": [
".",
"bazel-out/darwin-fastbuild/bin/*",
"bazel-out/k8-fastbuild/bin/*",
"bazel-out/x64_windows-fastbuild/bin/*",
"bazel-out/darwin-dbg/bin/*",
"bazel-out/k8-dbg/bin/*",
"bazel-out/x64_windows-dbg/bin/*"
]
},
"rootDirs": [
".",
"bazel-out/darwin-fastbuild/bin/",
"bazel-out/k8-fastbuild/bin/",
"bazel-out/x64_windows-fastbuild/bin/",
"bazel-out/darwin-dbg/bin/",
"bazel-out/k8-dbg/bin/",
"bazel-out/x64_windows-dbg/bin/"
]
...
}
...
}
Folder Structure:
apps
server
services
BUILD
hello_service.ts
BUILD
index.ts
libs
a_lib
BUILD
do_a.ts
b_lib
BUILD
do_b.ts
c_lib
BUILD
do_c.ts
Build
tsconfig.json
index.ts:
...
import { doToC } from '#libs/c_lib/do_c';
import {
HelloService,
HelloServiceImpl,
} from '#apps/server/services/hello_service';
...
Full Stacktrace:
INFO: Analyzed target //apps/server:server (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //apps/server:server up-to-date:
bazel-bin/apps/server/server.sh
bazel-bin/apps/server/server_loader.js
bazel-bin/apps/server/server_require_patch.js
INFO: Elapsed time: 1.994s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Error: Cannot find module '#libs/c_lib/do_c'
Require stack:
- /home/tim/.cache/bazel/_bazel_tim/4d04e993ef8ae996d277978eb4eab582/execroot/typescript/bazel-out/k8-fastbuild/bin/apps/server/server.sh.runfiles/typescript/apps/server/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (home/tim/.cache/bazel/_bazel_tim/4d04e993ef8ae996d277978eb4eab582/execroot/typescript/bazel-out/k8-fastbuild/bin/apps/apps/server/index.ts:4:1)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
Please see the example repo for an example of how to reproduce this error.

AWS Lambda function error: Cannot find module 'lambda'

I am trying to deploy a REST API in AWS using serverless. Node version 14.17.5.
My directory structure:
When I deploy the above successfully I get the following error while trying to access the api.
2021-09-28T18:32:27.576Z undefined ERROR Uncaught Exception {
"errorType": "Error",
"errorMessage": "Must use import to load ES Module: /var/task/lambda.js\nrequire() of ES modules is not supported.\nrequire() of /var/task/lambda.js from /var/runtime/UserFunction.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.\nInstead rename lambda.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.\n",
"code": "ERR_REQUIRE_ESM",
"stack": [
"Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/lambda.js",
"require() of ES modules is not supported.",
"require() of /var/task/lambda.js from /var/runtime/UserFunction.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.",
"Instead rename lambda.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.",
"",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)",
" at Module.load (internal/modules/cjs/loader.js:937:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
" at Module.require (internal/modules/cjs/loader.js:961:19)",
" at require (internal/modules/cjs/helpers.js:92:18)",
" at _tryRequire (/var/runtime/UserFunction.js:75:12)",
" at _loadUserApp (/var/runtime/UserFunction.js:95:12)",
" 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:1072:14)"
]
}
As per the suggestion in the error I tried changing the lambda.js to lambda.cjs. Now I get the following error
2021-09-28T17:32:36.970Z undefined ERROR Uncaught Exception {
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'lambda'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module 'lambda'",
"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:1072:14)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)",
" at Module.load (internal/modules/cjs/loader.js:937:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
" at internal/main/run_main_module.js:17:47"
]
}
serverless.yml
service: APINAME #Name of your App
useDotenv: true
configValidationMode: error
provider:
name: aws
runtime: nodejs14.x # Node JS version
memorySize: 512
timeout: 15
stage: dev
region: us-east-1 # AWS region
lambdaHashingVersion: 20201221
functions:
api:
handler: lambda.handler
events:
- http: ANY /{proxy+}
- http: ANY /
lambda.js
import awsServerlessExpress from 'aws-serverless-express'
import app from './index.js'
const server = awsServerlessExpress.createServer(app)
export const handler = (event, context) => {
awsServerlessExpress.proxy(server, event, context)
}
aws-cli commands
docker run --rm -it amazon/aws-cli --version
docker run --rm -it amazon/aws-cli configure
docker run --rm -it amazon/aws-cli serverless deploy
serverless commands:
docker run --rm -it amazon/aws-cli serverless deploy
serverless config credentials --provider aws --key <KEY> --secret <SECRET>
node ./node_modules/serverless/bin/serverless config credentials --provider aws --key <KEY> --secret <SECRET>
After reading up a couple of answers I have tried the following:
Made sure package.json includes "type": "module"
Deleted node_modules and package-lock.json and reinstalled all of them (since the version of node was updated during development)
What am I doing wrong?
Converted all imports to require() and all exports to module.exports
Removed "type": "module" from package.json
Everything works like a charm. It is not a solution to the question asked but making things work became more important.
Instead rename lambda.js to end in .cjs, change the requiring code to use
import(),
and remove
"type": "module"
from /var/task/package.json. It is there in the error.

Nuxt - Node error when I want to import some packages

I use Nuxt, and sometimes, when I want to use some npm packages, I have this error:
SyntaxError
Unexpected token '<'
The stack:
vm.js:102:7
new Script
internal/modules/cjs/loader.js:1114:10
Module._extensions..js
internal/modules/cjs/loader.js:950:32
Module.load
internal/modules/cjs/loader.js:790:14
Module._load
internal/modules/cjs/loader.js:974:19
Module.require
webpack:/external "vue-typeahead-bootstrap":1:
Object.vue-typeahead-bootstrap
webpack/bootstrap:25:
__webpack_require__
pages/account/tabs/addresses.js:693:81
Module../node_modules/babel-loader/lib/index.js?!./node_modules/vue-loader/lib/index.js?!./components/address/form/country.vue?vue&type=script&lang=js&
webpack/bootstrap:25:
I have this error for example on this package: vue-typeahead-bootstrap
If I import the package:
import VueTypeaheadBootstrap from ['vue-typeahead-bootstrap'](https://github.com/mattzollinhofer/vue-typeahead-bootstrap)
export default {
components: { VueTypeaheadBootstrap },
}
It throws the error.
Is it because the package is not supported or something ?
You may try to transpile it. https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-build#transpile
Add the package name like this
{
build: {
transpile: [
({ isServer }) => 'vue-typeahead-bootstrap'
]
}
}
As answered here: https://github.com/mattzollinhofer/vue-typeahead-bootstrap/issues/19#issuecomment-645510809

Semantic-release - Error: Cannot find module '../output'

No matter what I do I always get exactly the same error when I run command 'npx semantic-release --debug
' in BitBucket pipelines. Dry-run seems to run fine.
Thing I have already tried:
reinstall node modules
update packages
copy exactly the same configuration/packages from project with
working semantic-release
All the files are definitely there. Maybe it can't access?
here is a full debug report:
npx: installed 746 in 16.856s
[5:36:26 PM] [semantic-release] › ℹ Running semantic-release version 17.1.1
2020-06-25T17:36:26.883Z semantic-release:config load config from: /opt/atlassian/pipelines/agent/build/package.json
2020-06-25T17:36:26.887Z semantic-release:config options values: {
branches: [ 'master', 'develop' ],
repositoryUrl: 'git#$NEXUS_USERNAME.org:acurable/acupebble-g2-firmware.git',
tagFormat: 'v${version}',
plugins: [
[ '#semantic-release/commit-analyzer', [Object] ],
[ '#semantic-release/npm', [Object] ],
[ '#semantic-release/release-notes-generator', [Object] ],
'#semantic-release/changelog',
'#semantic-release/git'
],
_: [],
debug: true,
'$0': '/root/.npm/_npx/52/bin/semantic-release'
}
[5:36:27 PM] [semantic-release] › ✖ An error occurred while running semantic-release: Error: Cannot find module '../output'
Require stack:
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/index.js
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/index.js
- /opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/lib/prepare.js
- /opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/utils.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/get-config.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/cli.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/bin/semantic-release.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:952:15)
at Function.Module._load (internal/modules/cjs/loader.js:835:27)
at Module.require (internal/modules/cjs/loader.js:1012:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js:4:24)
at Module._compile (internal/modules/cjs/loader.js:1123:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
at Module.load (internal/modules/cjs/loader.js:972:32)
at Function.Module._load (internal/modules/cjs/loader.js:872:14)
at Module.require (internal/modules/cjs/loader.js:1012:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js',
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/index.js',
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/index.js',
'/opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/lib/prepare.js',
'/opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/utils.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/get-config.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/cli.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/bin/semantic-release.js'
]
}
Error: Cannot find module '../output'
Require stack:
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/index.js
- /opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/index.js
- /opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/lib/prepare.js
- /opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/utils.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/lib/get-config.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/index.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/cli.js
- /root/.npm/_npx/52/lib/node_modules/semantic-release/bin/semantic-release.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:952:15)
at Function.Module._load (internal/modules/cjs/loader.js:835:27)
at Module.require (internal/modules/cjs/loader.js:1012:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js:4:24)
at Module._compile (internal/modules/cjs/loader.js:1123:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
at Module.load (internal/modules/cjs/loader.js:972:32)
at Function.Module._load (internal/modules/cjs/loader.js:872:14)
at Module.require (internal/modules/cjs/loader.js:1012:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/output-json.js',
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/json/index.js',
'/opt/atlassian/pipelines/agent/build/node_modules/fs-extra/lib/index.js',
'/opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/lib/prepare.js',
'/opt/atlassian/pipelines/agent/build/node_modules/#semantic-release/changelog/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/utils.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/plugins/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/lib/get-config.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/index.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/cli.js',
'/root/.npm/_npx/52/lib/node_modules/semantic-release/bin/semantic-release.js'
]
2020-06-25T17:36:27.964997083Z stderr P }
This is crazy.
Just realised that I had this line in gitIgnore.txt: **/Output
In my local repository all the files were there but index.js was missing since all files in any **/Output path would not be included.
Will never use global gitIgnore again.

Resources