admin管理员组文章数量:1384341
Below has the details , tried multiple updates, still no luck works in VS code but not in gcp - cloud run package.json
{
"name": "whatsappendpoint-test2",
"type" : "module",
"engines": {
"node": ">=14.0.0"
},
"version": "0.0.1",
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0"
}
}
Code
export function test(req, res) {
import {MY_CONST} from './consts.js';
console.log(test)
res.send('Hello world');
};
BUILD ERROR
Running "node --check index.js"enter code here
/workspace/index.js:4
import {MY_CONST} from './consts.js'
^
SyntaxError: Unexpected token '{'
at checkSyntax (node:internal/main/check_syntax:74:5)
Node.js v22.14.0
Below has the details , tried multiple updates, still no luck works in VS code but not in gcp - cloud run package.json
{
"name": "whatsappendpoint-test2",
"type" : "module",
"engines": {
"node": ">=14.0.0"
},
"version": "0.0.1",
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0"
}
}
Code
export function test(req, res) {
import {MY_CONST} from './consts.js';
console.log(test)
res.send('Hello world');
};
BUILD ERROR
Running "node --check index.js"enter code here
/workspace/index.js:4
import {MY_CONST} from './consts.js'
^
SyntaxError: Unexpected token '{'
at checkSyntax (node:internal/main/check_syntax:74:5)
Node.js v22.14.0
Share
Improve this question
asked Mar 18 at 15:22
Praveen VPraveen V
551 gold badge1 silver badge9 bronze badges
1
|
1 Answer
Reset to default 1This bug is easy to explain if you look at the documentation on import
:
import
declarations can only be present in modules, and only at the top-level (i.e. not inside blocks, functions, etc.).
You can fix this bug by moving this line out of the function test
to the top-level scope, for example, to the top of the file:
import {MY_CONST} from './consts.js';
//...
// use MY_CONST
本文标签: javascriptES Modules JS not working in GCP functionsStack Overflow
版权声明:本文标题:javascript - ES Modules JS not working in GCP functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744505075a2609547.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
import {MY_CONST} from './consts.js';
isn't correct in that line. You can't have static imports inside function bodies. What does "works in VS code" mean? How do you run this invalid code in VS Code? – jabaa Commented Mar 18 at 15:30