Quantcast
Channel: Active questions tagged aws-lambda - Stack Overflow
Viewing all articles
Browse latest Browse all 4012

serverless-offline module --> Cannot use import statement outside a module

$
0
0

I am currently developing a simple lambda function using node.js with serverless.yml templates.

My project is using typescript and tsconfig is set to ESNext target version along with ESnext module as seen below

{"compilerOptions": {"preserveConstEnums": true,"strictNullChecks": true,"sourceMap": true,"allowJs": true,"resolveJsonModule": true,"esModuleInterop": true,"strict": true,"target": "ESNext","outDir": ".build","moduleResolution": "node","module": "ESNext","lib": ["ESNext", "DOM"], // DOM because @aws-sdk/client-s3 uses some of the DOM types that does not exist in node. This is the only fix I am able to find right now that suppresses all warnings.."rootDir": "./","typeRoots": ["./src/types", "./node_modules/@types"]  }}

The package.json's type is also set to module along with requiring node version to be >= 20.0.0

{"type": "module","ava": {"extensions": ["ts"    ],"timeout": "30s","require": ["ts-node/register"    ]  },"devDependencies": {"@types/adm-zip": "^0.5.5","@types/aws-lambda": "^8.10.136","@types/lodash": "^4.14.191","@types/node": "^14.18.7","@types/node-fetch": "^2.5.12","@types/uuid": "^8.3.4","@types/ws": "^8.5.10","ava": "^4.0.1","aws-sdk": "^2.1448.0","prettier": "^2.5.1","serverless": "^3.0.0","serverless-iam-roles-per-function": "^3.2.0","serverless-offline": "13.3.3","serverless-plugin-datadog": "^5.21.0","serverless-plugin-typescript": "^2.1.5","ts-node": "^10.4.0","typescript": "^5.4.2","ws": "^8.16.0"  },"dependencies": {"@turf/difference": "^6.5.0","@turf/helpers": "^6.5.0","@turf/union": "^6.5.0","adm-zip": "^0.5.12","fs": "^0.0.1-security","lodash": "^4.17.21","node-fetch": "^2.6.7","uuid": "^8.3.2","zod": "^3.22.3"  },"engines": {"node": ">=20.0.0"  }}

As you can see from devDependencies, it does require serverless-offline.

However, when the function is running locally using serverless-offline, the following error was hit.

[![UserFunction.js error][1]][1]

I already tried to compile everything into commonJs and that didn't work any better.

Not sure why this is the case. Has anyone who used serverless ran into this problem before?

Edit:Tried to change the moduleResolution and module to NodeNext. However, the following error is encountered.

[![enter image description here][2]][2]

Not sure why that would be the case. This is the same error I ran into when I set the module to commonJs. I checked the file in question (UserFunction.js). It is written with commonJs format, and I can also see a module.exports.load defined.[1]: https://i.stack.imgur.com/Evo66.png[2]: https://i.stack.imgur.com/eaXNO.png


Viewing all articles
Browse latest Browse all 4012

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>