Skip to content

Error trying to import ESM Temporal polyfill in a CJS environment #29

@justingrant

Description

@justingrant

This polyfill is marked as ESM in package..json via type: 'module', but this is causing an error at runtime in my code which runs in an AWS Lambda function. According to this Stack Overflow answer, AWS Lambda's Node 14 runtime does not support ESM at the moment (or at least didn't as of a few months ago).

It's possible (I haven't tested) that the workaround is what's described in dherault/serverless-offline#1014 (comment) might help:

It's possible to distribute both using exports. Old versions of Node will ignore it and fall back to main while newer versions will load the ES module if the caller supports it.

{
  "type": "module",
  "main": "dist/index.cjs",
  "exports": {
    "import": "./src/index.js",
    "require": "./dist/index.cjs"
  },
  "module": "index.js"
}

Below is the error I'm seeing at runtime when my code is run locally using the serverless-offline package, which is a harness for running AWS Lambda functions locally on my dev machine. This harness doesn't apparently support ESM either, per dherault/serverless-offline#1014.

If any experts in Node's ESM support, Webpack 5, and/or AWS Lambda have any suggestions, I'm all ears!

/usr/local/bin/node --lazy ./../../../../../../usr/local/lib/node_modules/serverless/bin/serverless offline start --noTimeout --dontPrintOutput --httpsProtocol .serverlessoffline

Error: Must use import to load ES Module: /Users/justingrant/Documents/hdev/h3/api/node_modules/@js-temporal/polyfill/dist/index.js
require() of ES modules is not supported.
require() of /Users/justingrant/Documents/hdev/h3/api/node_modules/@js-temporal/polyfill/dist/index.js from /Users/justingrant/Documents/hdev/h3/api/.webpack/service/src/getCurrentUser.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/justingrant/Documents/hdev/h3/api/node_modules/@js-temporal/polyfill/package.json.

    at new NodeError (node:internal/errors:370:5)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:13)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.@js-temporal/polyfill (/Users/justingrant/Documents/hdev/h3/api/.webpack/service/src/webpack:/h3-api/external "@js-temporal/polyfill":1:1)
    at __webpack_require__ (/Users/justingrant/Documents/hdev/h3/api/.webpack/service/src/webpack:/h3-api/webpack/bootstrap:19:1)
    at Object.../web/src/Serializer.ts (/Users/justingrant/Documents/hdev/h3/api/.webpack/service/src/getCurrentUser.js:1226:79)
    at __webpack_require__ (/Users/justingrant/Documents/hdev/h3/api/.webpack/service/src/webpack:/h3-api/webpack/bootstrap:19:1)
    at Object../src/libs/lambdaHandler.ts (/Users/justingrant/Documents/hdev/h3/api/.webpack/service/src/getCurrentUser.js:55:77)
    at __webpack_require__ (/Users/justingrant/Documents/hdev/h3/api/.webpack/service/src/webpack:/h3-api/webpack/bootstrap:19:1)
    at /Users/justingrant/Documents/hdev/h3/api/.webpack/service/src/getCurrentUser.js:2204:77
    at /Users/justingrant/Documents/hdev/h3/api/.webpack/service/src/getCurrentUser.js:2233:3
    at Object.<anonymous> (/Users/justingrant/Documents/hdev/h3/api/.webpack/service/src/getCurrentUser.js:2238:12)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at /Users/justingrant/Documents/hdev/h3/api/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:133
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at InProcessRunner.run (/Users/justingrant/Documents/hdev/h3/api/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:157:9)

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