Skip to content

Imports not resolving properly during runtime #329

Closed
@smasilamani-cfins

Description

@smasilamani-cfins

Hello

We are using this package for one for our serverless typescript projects and have been facing issue with the import. During runtime none of the imports from this package are resolving and we have to do a patch to make it work, Please find below the respective code and error we get unless we run the below command after npm install. Please advise.

Command to fix :
cp -r ./node_modules/@vendia/serverless-express/src/*.js ./node_modules/@vendia/serverless-express

If we run the above command after npm install, then it works fine.

Error:

TypeError: serverless_express_1.createServer is not a function
    at bootstrapServer (/mnt/c/Repos/iq-submission-service/.webpack/service/src/webpack:/iq-submission-service/src/index.ts:38:10)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)
    at handler (/mnt/c/Repos/iq-submission-service/.webpack/service/src/webpack:/iq-submission-service/src/index.ts:77:18)
    at InProcessRunner.run (/mnt/c/Repos/iq-submission-service/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:199:24)
    at LambdaFunction.runHandler (/mnt/c/Repos/iq-submission-service/node_modules/serverless-offline/dist/lambda/LambdaFunction.js:325:20)
    at hapiHandler (/mnt/c/Repos/iq-submission-service/node_modules/serverless-offline/dist/events/http/HttpServer.js:521:18)
    at module.exports.internals.Manager.execute (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/toolkit.js:45:28)
    at Object.internals.handler (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/handler.js:46:20)
    at exports.execute (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/handler.js:31:20)
    at Request._lifecycle (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/request.js:312:32)
    at Request._execute (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/request.js:221:9)
TypeError: serverless_express_1.createServer is not a function
    at bootstrapServer (/mnt/c/Repos/iq-submission-service/.webpack/service/src/webpack:/iq-submission-service/src/index.ts:38:10)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)
    at handler (/mnt/c/Repos/iq-submission-service/.webpack/service/src/webpack:/iq-submission-service/src/index.ts:77:18)
    at InProcessRunner.run (/mnt/c/Repos/iq-submission-service/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:199:24)
    at LambdaFunction.runHandler (/mnt/c/Repos/iq-submission-service/node_modules/serverless-offline/dist/lambda/LambdaFunction.js:325:20)
    at hapiHandler (/mnt/c/Repos/iq-submission-service/node_modules/serverless-offline/dist/events/http/HttpServer.js:521:18)
    at module.exports.internals.Manager.execute (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/toolkit.js:45:28)
    at Object.internals.handler (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/handler.js:46:20)
    at exports.execute (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/handler.js:31:20)
    at Request._lifecycle (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/request.js:312:32)
    at Request._execute (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/request.js:221:9)
offline: Failure: serverless_express_1.createServer is not a function
TypeError: serverless_express_1.createServer is not a function
    at bootstrapServer (/mnt/c/Repos/iq-submission-service/.webpack/service/src/webpack:/iq-submission-service/src/index.ts:38:10)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)
    at handler (/mnt/c/Repos/iq-submission-service/.webpack/service/src/webpack:/iq-submission-service/src/index.ts:77:18)
    at InProcessRunner.run (/mnt/c/Repos/iq-submission-service/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:199:24)
    at LambdaFunction.runHandler (/mnt/c/Repos/iq-submission-service/node_modules/serverless-offline/dist/lambda/LambdaFunction.js:325:20)
    at hapiHandler (/mnt/c/Repos/iq-submission-service/node_modules/serverless-offline/dist/events/http/HttpServer.js:521:18)
    at module.exports.internals.Manager.execute (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/toolkit.js:45:28)
    at Object.internals.handler (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/handler.js:46:20)
    at exports.execute (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/handler.js:31:20)
    at Request._lifecycle (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/request.js:312:32)
    at Request._execute (/mnt/c/Repos/iq-submission-service/node_modules/@hapi/hapi/lib/request.js:221:9)

Webpack:

const path = require('path');
const slsw = require('serverless-webpack');
const TsConfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const entries = {};
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const HappyPack = require('happypack');

Object.keys(slsw.lib.entries).forEach((key) => (entries[key] = ['./source-map-install.js', slsw.lib.entries[key]]));

module.exports = {
	mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
	entry: entries,
	devtool: 'source-map',
	optimization: {
		minimizer: [
			new TerserPlugin({
				terserOptions: {
					mangle: true,
					keep_classnames: true,
					sourceMap: true
				},
				parallel: true
			})
		]
	},
	plugins: [
		new ForkTsCheckerWebpackPlugin({ typescript: { memoryLimit: 4096 } }),
		new HappyPack({
			id: 'ts',
			threads: 10,
			loaders: [
				{
					path: 'ts-loader',
					query: { happyPackMode: true }
				}
			]
		})
	],
	resolve: {
		plugins: [
			new TsConfigPathsPlugin({
				configFile: './tsconfig.json'
			})
		],
		extensions: ['.js', '.jsx', '.json', '.ts', '.tsx']
	},
	output: {
		libraryTarget: 'commonjs',
		path: path.join(__dirname, '.webpack'),
		filename: '[name].js'
	},
	target: 'node',
	module: {
		rules: [
			// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
			{
				test: /\.ts|\.tsx$/,
				loader: 'ts-loader',
				exclude: [/node_modules/],
				options: {
					// disable type checker - we will use it in fork plugin
					transpileOnly: true
				}
				// include: __dirname
			}
		]
	},
	externals: [
		'oracledb',
		'mongodb',
		'mysql',
		'pg',
		'pg-native',
		'pg-query-stream',
		'redis',
		'sqlite3',
		'mssql',
		'mysql2',
		'@sap/hana-client',
		'hdb-pool',
		'typeorm-aurora-data-api-driver',
		'better-sqlite3',
		'react-native-sqlite-storage',
		'sql.js',
		'grpc',
		'kafkajs',
		'mqtt',
		'nats',
		'amqplib',
		'amqp-connection-manager',
		'fastify-swagger'
	],
	ignoreWarnings: [
		// This is optional, but it hides noisey warnings
		{ module: /node_modules\/*/, message: /Critical dependency: the request of a dependency is an expression/ },
		(warning) => false
	]
};

Code: (only the interested part)

import { Handler } from 'aws-lambda';
import Express from 'express';
import { Server } from 'http';

import { INestApplication } from '@nestjs/common';
import { ExpressAdapter } from '@nestjs/platform-express';
import { DSServerConstants } from '@shared/models/server.constants';
import { createServer, proxy } from '@vendia/serverless-express';

import { bootstrap } from './main';

let cachedServer: Server;

const binaryMimeTypes: string[] = [
	'application/javascript',
	'application/octet-stream',
	'application/xml',
	'font/eot',
	'font/opentype',
	'font/otf',
	'image/jpeg',
	'image/png',
	'image/svg+xml',
	'text/comma-separated-values',
	'text/css',
	'text/html',
	'text/javascript',
	'text/plain',
	'text/text',
	'text/xml'
];

const bootstrapServer: () => Promise<Server> = async () => {
	try {
		const expressApp: any = Express();
		const expAdapter: ExpressAdapter = new ExpressAdapter(expressApp);
		const app: INestApplication = await bootstrap(expAdapter);
		return createServer(app.getHttpAdapter().getInstance(), null, binaryMimeTypes);
	} catch (error) {
		console.error(error);
		throw error;
	}
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions