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
  • 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
Add a comment  | 

1 Answer 1

Reset to default 1

This 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