serverless-esbuild icon indicating copy to clipboard operation
serverless-esbuild copied to clipboard

Serverless-esbuild incorrectly importing .js files when .mjs output is specified.

Open jtoronto opened this issue 2 years ago • 2 comments

Describe the bug Following the advice here: https://github.com/oieduardorabelo/top-level-await-in-aws-lamba-with-typescript

I added the following to my esbuild config.

    format: 'esm'
    outputFileExtension: '.mjs'
    platform: 'node'
    target: 'esnext'   

The deployed function errors when invoked because it's unable to find it. ERROR Error: Cannot find module './lib/utility/error_throwing_function.js'

If I just run sls package and inspect the contents, this is what I find.

s_v1SNSErrorReporterTester.js:


var serverlessSDK = require('./serverless_sdk/index.js');
serverlessSDK = new serverlessSDK({
  orgId: 'REDACTED',
  applicationName: 'api',
  appUid: 'REDACTED',
  orgUid: 'REDACTED',
  deploymentUid: 'REDACTED',
  serviceName: 'api',
  shouldLogMeta: true,
  shouldCompressLogs: true,
  disableAwsSpans: false,
  disableHttpSpans: false,
  stageName: 'dev',
  serverlessPlatformStage: 'prod',
  devModeEnabled: false,
  accessKey: null,
  pluginVersion: '6.2.2',
  disableFrameworksInstrumentation: false
});

const handlerWrapperArgs = { functionName: 'api-dev-v1SNSErrorReporterTester', timeout: 6 };

try {
  const userHandler = require('./lib/utility/error_throwing_function.js');
  module.exports.handler = serverlessSDK.handler(userHandler.handler, handlerWrapperArgs);
} catch (error) {
  module.exports.handler = serverlessSDK.handler(() => { throw error }, handlerWrapperArgs);
}

Note that it's still trying to require it from its former .js extension, however if you do look in ./lib/utility of the package, error_throwing_function is indeed named .mjs. To Reproduce Steps to reproduce the behavior. If you have an example repository that would be even better:

  1. set outputFileExtension: to '.mjs'
  2. Run sls package
  3. Inspect contents.

Expected behavior require should point to .mjs file instead of .js file.

Versions (please complete the following information):

  • OS: Mac
  • Serverless Framework Version: [e.g. 3.23.0]
  • Plugin Version: [e.g. 1.32.8]

Additional context Add any other context about the problem here.

jtoronto avatar Nov 01 '22 00:11 jtoronto

+1

IslamWahid avatar Nov 10 '22 14:11 IslamWahid

This issue makes esbuild usage impossible

Sceat avatar Jun 13 '23 08:06 Sceat