What did I do to cause this error: "SyntaxError: Cannot use import statement outside a module"

Viewed 19

I already know the solution to this problem is to add "type":"module" to the package.json file but I don't want to do that since it gives me more errors with one of the dependencies I am using.

I just want to understand what I did to cause this error, since it was working fine for me and then all of a sudden I am getting this error.

Thanks in advance

package.json:

{
  "name": "<project-name>",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "devStart": "nodemon --exec babel-node src/index.js",
    "normalRun": "node src/index.js"
  },
  "author": "<author-name>",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.18.10",
    "@babel/core": "^7.18.10",
    "@babel/node": "^7.18.10",
    "@babel/preset-env": "^7.18.10",
    "nodemon": "^2.0.19"
  },
  "dependencies": {
    "docx": "^7.4.1",
    "express": "^4.18.1"
  }
}

index.js:

import express, { json } from "express";
import { create } from "./routes/create";

const app = express();
const port = 3535;

app.use(json());

app.get("/", (req, res) => {
  res.send("Hello There, try sending a post request to /create.");
});

app.post("/create", (req, res) => {
  const debug_list = create(req.body["document"]["nodes"]);
  res.send(debug_list);
});

app.listen(port, () => {
  console.log(`Listening on ${port}`);
});
0 Answers
Related