Skip to content

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

@liudonghua123

Description

@liudonghua123

我创建了一个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}

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