我可以在IBM云功能(node.js)中使用ES模块,还是仅支持常见的JS?
当我从包含一个简单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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在进行了更多研究之后,我得出结论,该动作本身不能是ES模块。
我尝试将其重命名为
estest.mjs
(没有“ type”:“ module”
inpackage.json
),我相信这是由于此
require()
inrunner.js
(IBM Cloud/OpenWhisk实现,它试图加载我的estest.mjs
ES模块):require()
不支持ES模块: https:// nodejs。 org/api/esm.html#esm_require 。解决
错误消息建议的解决方案是,解决方案是创建一个CommonJs包装器(
estest.cjs
),该解决方案是我的ES模块estest的动态
。 mjs 并使用import()
import()“ main”:“ estest.cjs”
inpackage.json
:estest.cjs
estest.mjs
package.json
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"
inpackage.json
) which gave:which I believe is due to this
require()
inrunner.js
(the IBM Cloud / OpenWhisk implementation which tries to load myestest.mjs
ES Module):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 dynamicimport()
of my ES moduleestest.mjs
and use"main": "estest.cjs"
inpackage.json
:estest.cjs
estest.mjs
package.json
我认为该线索是错误消息中:
您是否尝试将扩展名更改为
wassed.est.est.mjs
?也可能值得在
package.json
中指定节点版本。I think the clue is in the error message:
have you tried changing the extension to
supported.estest.mjs
?It might also be worth specifying the node version in
package.json
eg.