Cannot use import statement outside a module.
nakulnagariy opened this issue · comments
Describe the bug
I 'have build my application using your template, app is working fine in local, but when I'm trying to run this using serverless.yml and sls offline it says Cannot use import statement outside a module
.
I'm not sure why this problem occurs but this is blocking me deploying my app.
Please suggest a way out.
serverless.yml
org: nakulnagariy
app: legalyatra-service
service: legalyatra-service
frameworkVersion: '3'
plugins:
- serverless-plugin-typescript
- serverless-plugin-optimize
- serverless-offline
- serverless-dotenv-plugin
# https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml
useDotenv: true
custom:
dotenv:
path: .env-${stage}
serverless-offline:
port: 4000
optimize:
external: ['swagger-ui-dist']
provider:
name: aws
runtime: nodejs18.x
region: ap-south-1
stackName: legalyatra-services-api
memorySize: 1024
timeout: 60
stage: 'dev'
package:
patterns:
- 'src/**'
- '!test/**'
functions:
main:
handler: ./src/lambda.handler
events:
- http:
path: /
method: ANY
cors: true
- http:
path: '{proxy+}'
method: ANY
cors: true
lamda.ts
import { Handler, Context } from 'aws-lambda';
import { Server } from 'http';
import { createServer, proxy } from 'aws-serverless-express';
import { eventContext } from 'aws-serverless-express/middleware';
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import express from 'express';
// NOTE: If you get ERR_CONTENT_DECODING_FAILED in your browser, this is likely
// due to a compressed response (e.g. gzip) which has not been handled correctly
// by aws-serverless-express and/or API Gateway. Add the necessary MIME types to
// binaryMimeTypes below
const binaryMimeTypes: string[] = [];
let cachedServer: Server;
async function bootstrapServer(): Promise<Server> {
if (!cachedServer) {
const expressApp = express();
const nestApp = await NestFactory.create(
AppModule,
new ExpressAdapter(expressApp),
);
nestApp.use(eventContext());
await nestApp.init();
cachedServer = createServer(expressApp, undefined, binaryMimeTypes);
}
return cachedServer;
}
export const handler: Handler = async (event: any, context: Context) => {
if (event.path === '/api') {
event.path = '/api/';
}
event.path = event.path.includes('swagger-ui')
? `/api${event.path}`
: event.path;
cachedServer = await bootstrapServer();
return proxy(cachedServer, event, context, 'PROMISE').promise;
};
To Reproduce
Steps to reproduce the behavior:
- Go to '...'
- Send '....'
- See error
Expected behavior
A clear and concise description of what you expected to happen.
Desktop (please complete the following information):
- OS: [e.g. Windows]
- NodeJS Version [e.g. 18.16.0]
- Database [e.g. PostgreSQL]
Additional context
Add any other context about the problem here.