繁体   English   中英

即使我在 Google App Engine 中设置了 {"type": "module"},也会出现“无法在模块外使用 import 语句”错误

[英]"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 解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM