我的代碼在本地運行,但當我嘗試在 Google App Engine (GAE) 上運行時不起作用。我 99.99% 確定它與我在本地推送到 git 然后在 GAE 中拉入的代碼相同。我肯定已經{"type": "module"}在我的package.json.
錯誤
import express from 'express';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:891:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
開始 package.json
{
"name": "njs",
"version": "1.0.0",
"description": "nodejs backend server",
"main": "app.js",
"type": "module",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1",
"live": "nodemon app.js"
},
"dependencies": {
"@grpc/grpc-js": "^1.1.0",
"@grpc/proto-loader": "^0.5.0",
"axios": "^0.21.1",
"express": "^4.16.1",
"google-protobuf": "^3.0.0",
"protobufjs": "~6.11.2",
"nodemon": "^2.0.13"
}
代碼
'use strict';
import express from 'express';
const app = express();
import cors from 'cors';
import {request} from 'http';
import * as db from './firestore.js';
import * as grpc from './grpc.js';
import * as validate from './validation.js';
app.use(cors(), (req, res, next) => {
console.log(req.method);
next();
});
// whole bunch of app.get() app.all() etc
const port = 8080;
app.listen(port, () => {
console.log('Server running on port ' port);
});
uj5u.com熱心網友回復:
使用nvm install 14從 v12.14.1 到 v14.18.0更新 cloudshell 中的節點修復了該問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/318971.html
