"Cannot use import statement outside a module" 错误,即使我在 Google App Engine 中设置了 {"type": "module"}
"Cannot use import statement outside a module" error even when I have {"type": "module"} set in Google App Engine
我的代码可以在本地运行,但当我在 Google App Engine(GAE) 上尝试 运行 时却不行。我 99.99% 确定它与我在本地推送到 git 然后将其拉入 GAE 中的代码相同。我的 package.json
.
中确实设置了 {"type": "module"}
错误
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);
});
使用 nvm install 14
将 cloudshell 中的节点从 v12.14.1 更新到 v14.18.0 解决了这个问题。
我的代码可以在本地运行,但当我在 Google App Engine(GAE) 上尝试 运行 时却不行。我 99.99% 确定它与我在本地推送到 git 然后将其拉入 GAE 中的代码相同。我的 package.json
.
{"type": "module"}
错误
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);
});
使用 nvm install 14
将 cloudshell 中的节点从 v12.14.1 更新到 v14.18.0 解决了这个问题。