Home > Software design >  "Cannot use import statement outside a module" error even when I have {"type": &
"Cannot use import statement outside a module" error even when I have {"type": &

Time:10-12

My code works locally but not when I try running it on Google App Engine(GAE). I'm 99.99% sure it's the same code as I pushed to git locally and then pulled it in in GAE. I definitely have {"type": "module"} set in my package.json.

The error

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

Start of 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"
  }

Code

'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);
});

CodePudding user response:

Updating node in cloudshell using nvm install 14 from v12.14.1 to v14.18.0 fixed the issue.

  • Related