我可以在IBM云功能(node.js)中使用ES模块,还是仅支持常见的JS?

发布于 2025-02-12 03:38:36 字数 1246 浏览 3 评论 0原文

当我从包含一个简单estest.js的zip文件创建node.js v16函数操作时:

function main(params) {
    return { message: 'Hello World' };
}

package.json包含“ type”:“ type”:“ module” 我得到:

{
  "error": "Initialization has failed due to: Error [ERR_REQUIRE_ESM]: require() of ES Module /nodejsAction/1AfjbP59/estest.js from /nodejsAction/runner.js not supported.estest.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.\nInstead rename estest.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 /nodejsAction/1AfjbP59/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).\n\n    at eval (eval at <anonymous> (/nodejsAction/runner.js:51:31), <anonymous>:1:1)\n    at /nodejsAction/runner.js:51:31"
}

我还没有找到任何IBM云文档,该文档指出只有旧版JS样式才得到支持。

编辑:

使用:

ibmcloud fn action create test/estest estest.zip --kind nodejs:16

When I create a Node.js v16 function action from a zip file containing a simple estest.js:

function main(params) {
    return { message: 'Hello World' };
}

and package.json containing "type": "module"
I get:

{
  "error": "Initialization has failed due to: Error [ERR_REQUIRE_ESM]: require() of ES Module /nodejsAction/1AfjbP59/estest.js from /nodejsAction/runner.js not supported.estest.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.\nInstead rename estest.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 /nodejsAction/1AfjbP59/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).\n\n    at eval (eval at <anonymous> (/nodejsAction/runner.js:51:31), <anonymous>:1:1)\n    at /nodejsAction/runner.js:51:31"
}

I have not found any IBM Cloud documentation which states that only the legacy Common JS style is supported.

EDIT:

Action created using:

ibmcloud fn action create test/estest estest.zip --kind nodejs:16

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2025-02-19 03:38:36

在进行了更多研究之后,我得出结论,该动作本身不能是ES模块。

我尝试将其重命名为estest.mjs(没有“ type”:“ module” in package.json),

{"error": "Initialization has failed due to: Error [ERR_REQUIRE_ESM]: require() of ES Module /nodejsAction/azrfHRlh/estest.mjs not supported.\nInstead change the require of /nodejsAction/azrfHRlh/estest.mjs to a dynamic import() which is available in all CommonJS modules.\n    at eval (eval at <anonymous> (/nodejsAction/runner.js:51:31), <anonymous>:1:1)\n    at /nodejsAction/runner.js:51:31"}

我相信这是由于此require() in runner.js(IBM Cloud/OpenWhisk实现,它试图加载我的estest.mjs ES模块):

//  The module to require.
let whatToRequire = index !== undefined ? path.join(moduleDir, index) : moduleDir;
let handler = eval('require("' + whatToRequire + '").' + main);

require()不支持ES模块: https:// nodejs。 org/api/esm.html#esm_require

解决

错误消息建议的解决方案是,解决方案是创建一个CommonJs包装器(estest.cjs),该解决方案是我的ES模块estest的动态import() import()。 mjs 并使用“ main”:“ estest.cjs” in package.json

estest.cjs
async function main(params) {
    const { mjstest } = await import('./estest.mjs');
    return mjstest(params);
}

exports.main = main;
estest.mjs
export const mjstest = (params) => {
    return { message: `Hello ${params.name || 'World'}` };
}
package.json
{
    "name": "estest",
    "version": "1.0.0",
    "main": "estest.cjs"
}

After some more research I've concluded the action itself can't be an ES Module.

I tried renaming it to estest.mjs (without "type":"module" in package.json) which gave:

{"error": "Initialization has failed due to: Error [ERR_REQUIRE_ESM]: require() of ES Module /nodejsAction/azrfHRlh/estest.mjs not supported.\nInstead change the require of /nodejsAction/azrfHRlh/estest.mjs to a dynamic import() which is available in all CommonJS modules.\n    at eval (eval at <anonymous> (/nodejsAction/runner.js:51:31), <anonymous>:1:1)\n    at /nodejsAction/runner.js:51:31"}

which I believe is due to this require() in runner.js (the IBM Cloud / OpenWhisk implementation which tries to load my estest.mjs ES Module):

//  The module to require.
let whatToRequire = index !== undefined ? path.join(moduleDir, index) : moduleDir;
let handler = eval('require("' + whatToRequire + '").' + main);

require() does not support ES modules: https://nodejs.org/api/esm.html#esm_require.

Solution

As the error message suggests, the solution was to create a CommonJS wrapper (estest.cjs) which does a dynamic import() of my ES module estest.mjs and use "main": "estest.cjs" in package.json:

estest.cjs
async function main(params) {
    const { mjstest } = await import('./estest.mjs');
    return mjstest(params);
}

exports.main = main;
estest.mjs
export const mjstest = (params) => {
    return { message: `Hello ${params.name || 'World'}` };
}
package.json
{
    "name": "estest",
    "version": "1.0.0",
    "main": "estest.cjs"
}
街角卖回忆 2025-02-19 03:38:36

我认为该线索是错误消息中:

supported.estest.js is treated as an ES module file as it is a .js file

您是否尝试将扩展名更改为wassed.est.est.mjs

也可能值得在package.json中指定节点版本。

  "engines": {
    "node": ">=16.x"
  },

I think the clue is in the error message:

supported.estest.js is treated as an ES module file as it is a .js file

have you tried changing the extension to supported.estest.mjs ?

It might also be worth specifying the node version in package.json eg.

  "engines": {
    "node": ">=16.x"
  },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文