serverless / serverless/serverless

Node ES6 support issue

Open
#12,118 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

v3
Dominant language
JavaScript
Stars
46.9k
Forks
5.7k
Avg merge
10h 7m
Merged PRs (30d)
57

Description

Are you certain it's a bug?
  • Yes, it looks like a bug
Is the issue caused by a plugin?
  • It is not a plugin issue
Are you using the latest v3 release?
  • Yes, I'm using the latest v3 release
Is there an existing issue for this?
  • I have searched existing issues, it hasn't been reported yet
Issue description

Using nodejs18.x runtime I have an issue with ES6 and Common JS.

When not setting type: module in package.json Lambda complains about using import statements outside of the module.export

SyntaxError: Cannot use import statement outside a module

But when I do set the type to module in package.json it complains that I am using required. Which I am not doing, but the s_tag_file.js file which is created by Serverless does contain required

{
    "errorType": "ReferenceError",
    "errorMessage": "require is not defined in ES module scope, you can use import instead\nThis file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.",
    "stack": [
        "ReferenceError: require is not defined in ES module scope, you can use import instead",
        "This file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.",
        "    at file:///var/task/s_tag_file.js:2:21",
        "    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)"
    ]
}

I would expect the file created by Serverless to also be in the ES6 notation, but it is not.

Here is my full code:

package.json:

{
  "dependencies": {
    "@aws-sdk/client-s3": "^3.391.0",
    "aws-sdk": "^2.1437.0"
  },
  "type": "module"
}

index.js:

import { S3Client, PutObjectTaggingCommand } from "@aws-sdk/client-s3";

export async function tagFile(event, context, callback) {
  const response = event.Records[0].cf.response;

  const client = new S3Client({ region: "eu-west-1"});
  const input = {
    Bucket: "BUCKET_NAME",
    Key: event.Records[0].cf.request.uri,
    Tagging: {
      TagSet: [
        {
          Key: "requestTime",
          Value: Date.now().toString(),
        },
      ],
    },
  };
  const command = new PutObjectTaggingCommand(input);
  const S3response = await client.send(command);
  console.log(S3response);

  callback(null, response);
};
Service configuration (serverless.yml) content
org: OUR_ORG
app: OUR_APP
service: OUR_SERVICE

frameworkVersion: '3'


provider:
  name: aws
  runtime: nodejs18.x
  lambdaHashingVersion: 20201221
  region: us-east-1 # Required for using CloudFront, region does not matter though (it runs at the CF edge location anyways)
  iamRoleStatements:
  cloudFront:
    cachePolicies:
      cdnCachePolicy:
        MinTTL: 0
        MaxTTL: 5184001
        DefaultTTL: 5184000
        ParametersInCacheKeyAndForwardedToOrigin:
          CookiesConfig:
            CookieBehavior:
              none
          EnableAcceptEncodingBrotli: true
          EnableAcceptEncodingGzip: true
          HeadersConfig:
            HeaderBehavior: none
          QueryStringsConfig:
            QueryStringBehavior: none

custom:
  origin: 'ORIGIN_URL'
  path: 'build'

functions:
  tag-file:
    handler: index.tagFile
    events:
      - cloudFront:
          eventType: viewer-response
          origin:
            DomainName: ${self:custom.origin}
            OriginPath: /${self:custom.path}
            CustomOriginConfig:
              OriginProtocolPolicy: match-viewer
          cachePolicy:
            name: cdnCachePolicy
Command name and used flags

N/A

Command output
N/A
Environment information
Framework Core: 3.29.0
Plugin: 6.2.3
SDK: 4.3.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the deployment using package.json with "type": "module", index.js, and the serverless.yml configuration, then inspect the generated s_tag_file.js. Trace how Serverless creates that file and compare its module syntax with the user's ES module entry point. Done means the generated artifact deploys without the reported require/import errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, javascript, node.js
Domain
build-system, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.