TencentCloud / TencentCloud/tencentcloud-sdk-nodejs

腾讯云函数不支持ESM, `Error [ERR_REQUIRE_ESM]: require() of ES Module /var/user/index.js from /var/runtime/node18/UserFunction.js not supported.`

Open
#148 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
600
Forks
113
Avg merge
6h 46m
Merged PRs (30d)
1

Description

我创建了一个node 18的云函数,修改代码如下,并且也添加了package.json,在index.js同级或者父目录src同级,设置 "type": "module",可以部署成功,但是测试的时候提示 Error [ERR_REQUIRE_ESM]: require() of ES Module /var/user/index.js from /var/runtime/node18/UserFunction.js not supported.

export const main_handler = async (event, context) => {
    console.log("Hello World")
    console.log(event)
    console.log(context)
    console._stdout.write(JSON.stringify({"aaa": "test value 1","bbb": "test value 2"})+'\n');
    const res = await fetch('https://www.baidu.com')
    const body = await res.text()
    console.info(body)
    return body
};
Invoke function helloworld-1709020351 [RequestId:1d85a4d7-3b1f-45a8-be7a-09dae72fc615]
Response: 
{"errorCode":1,"errorMessage":"Error [ERR_REQUIRE_ESM]: require() of ES Module /var/user/index.js from /var/runtime/node18/UserFunction.js not supported.
Instead change the require of index.js in /var/runtime/node18/UserFunction.js to a dynamic import() which is available in all CommonJS modules.
    at module.exports.load (/var/runtime/node18/UserFunction.js:26:19)
    at Runtime.handleOnce (/var/runtime/node18/Runtime.engine.js:85:38)
    at Timeout._onTimeout (/var/runtime/node18/Runtime.engine.js:54:12)","requestId":"1d85a4d7-3b1f-45a8-be7a-09dae72fc615","statusCode":443}
Output:

Summary:
  Request ID: 1d85a4d7-3b1f-45a8-be7a-09dae72fc615
  Run Duration: 0
  Bill Duration: 0
  Usage Memory: 0
  Invoke Result: -1

希望node 20的runtime也可以提供一下,这个版本添加了一些需要的特性并且早已是LTS版本了.

另外如果代码是cjs,package.json里面设置了"type": "module",则提示下面的错误,说明这个配置是生效的,建议UserFunction.js里面的require可以替换成import,这样同时兼容cjs和mjs。

Invoke function helloworld-1709020351 [RequestId:711f1ade-4eb2-4127-b3a9-8af6d8f4fd3f]
Response: 
{"errorCode":1,"errorMessage":"Error [ERR_REQUIRE_ESM]: require() of ES Module /var/user/index.js from /var/runtime/node18/UserFunction.js not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which declares all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change \"type\": \"module\" to \"type\": \"commonjs\" in /var/user/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at module.exports.load (/var/runtime/node18/UserFunction.js:26:19)
    at Runtime.handleOnce (/var/runtime/node18/Runtime.engine.js:85:38)
    at Timeout._onTimeout (/var/runtime/node18/Runtime.engine.js:54:12)","requestId":"711f1ade-4eb2-4127-b3a9-8af6d8f4fd3f","statusCode":443}

Contributor guide

No contributing guide indexed for this repository

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

Start with the reported /var/runtime/node18/UserFunction.js load path and the Node.js module-resolution behavior described in the error. Reproduce the function with package.json set to type: module and verify how the runtime handles index.js and the requested Node 20 runtime. Done means the documented ESM function deploys and invokes successfully without breaking CommonJS functions.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
backend, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.