serverless: [v3] Node v22.12.0 Error _serverlessExternalPluginName

Issue description

Just getting this error while running v2 on the node release v22.12.0

TypeError: Cannot redefine property: _serverlessExternalPluginName
     at Function.defineProperty (<anonymous>)
     at PluginManager.resolveServicePlugins (/api/node_modules/serverless/lib/classes/plugin-manager.js:230:14)
     at async PluginManager.loadAllPlugins (/api/node_modules/serverless/lib/classes/plugin-manager.js:136:36)
     at async Serverless.init (/api/node_modules/serverless/lib/serverless.js:146:5)
     at async /api/node_modules/serverless/scripts/serverless.js:601:7

Context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 13
  • Comments: 15 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Hey everyone, we’ve just released v3.40.0, which resolves the Node v22.12 incompatibility. Feel free to update and give it a try.

Hey, this issue is caused by Node v22.12 due to a change in how it loads native ES modules

Support for loading native ES modules using require() had been available on v20.x and v22.x under the command line flag --experimental-require-module, and available by default on v23.x. In this release, it is now no longer behind a flag on v22.x.

With this feature enabled, Node.js will no longer throw ERR_REQUIRE_ESM if require() is used to load a ES module.

As a workaround, you can disable this experimental feature by setting the following environment variable before running Serverless:

export NODE_OPTIONS="--no-experimental-require-module"

We’re seeing ERR_REQUIRE_ASYNC_MODULE thrown at line 5 in require-with-import-fallback.js

RESOLVED: We resolved by disabling both experiments:

export NODE_OPTIONS="--no-experimental-detect-module --no-experimental-require-module"

This also happens with 3.40.0. Adding NODE_OPTIONS=--no-experimental-require-module before sls package doesn’t fix it but it changes the error.

I wonder if the problem we are seeing is triggered by the experimental detect module flag that was enabled by default as of Node 22.7.0.

https://nodejs.org/api/cli.html#--no-experimental-detect-module

This seems to pose a problem for TypeScript:

https://github.com/microsoft/TypeScript/issues/56678

Our scenario may be the one described here:

https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V22.md#requireesm-is-now-enabled-by-default

Any insight would be appreciated.

4962 | Environment: linux, node 22.13.0, framework 3.39.0 (local), plugin 7.2.3, SDK 4.5.1
4963 | Docs:        docs.serverless.com
4964 | Support:     forum.serverless.com
4965 | Bugs:        github.com/serverless/serverless/issues
4966 |  
4967 | Error:
4968 | Error [ERR_REQUIRE_ASYNC_MODULE]: require() cannot be used on an ESM graph with top-level await.
  Use import() instead. To see where the top-level await comes from, use --experimental-print-required-tla.
4969 | at ModuleJobSync.runSync (node:internal/modules/esm/module_job:392:13)
4970 | at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:329:47)
4971 | at loadESMFromCJS (node:internal/modules/cjs/loader:1411:24)
4972 | at Module._compile (node:internal/modules/cjs/loader:1544:5)
4973 | at Object..js (node:internal/modules/cjs/loader:1668:16)
4974 | at Module.load (node:internal/modules/cjs/loader:1313:32)
4975 | at Function._load (node:internal/modules/cjs/loader:1123:12)
4976 | at TracingChannel.traceSync (node:diagnostics_channel:322:14)
4977 | at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
4978 | at Module.require (node:internal/modules/cjs/loader:1335:12)
4979 | at require (node:internal/modules/helpers:136:16)
4980 | at module.exports (xxx/node_modules/serverless/lib/utils/require-with-import-fallback.js:5:12)

This applies to node 20.19.x and Serverless 3.39 and 3.40. For us, rolling back to node 20.18.1 solved the problem.

The links to other tickets may make that clear, but just to be pretty explicit, as this is the only ticket that comes up on Google searches for me about this and the title may make you think otherwise.

node LTS version 20.18.1 works.

Hi @eudoroolivares2016, Nope. This appears to be an issue related to a (breaking?) change in Node.js V22, distributed in one of its minor releases. A few other projects seem affected. We’ll continue to look into this.

I can see that this works on node version 22.11.0 and breaks on versions 22.12.0.