5

I have been having issues with node js.

Here is the relevant code (app.ts):

const express = require("express");
const auth = require("./service/auth");

import { Request, Response } from "express";

import User from "./model/User";
import con from "./config/db";

const port = 3000;

const app = express();

app.use(express.json());
...

The error I get is:

(node:16524) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\010bo\OneDrive\Documents\GitHub\rpg-backend\app\app.ts:4
import { Request, Response } from "express";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1117:16)
    at Module._compile (internal/modules/cjs/loader.js:1165:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
    at Module.load (internal/modules/cjs/loader.js:1050:32)
    at Function.Module._load (internal/modules/cjs/loader.js:938:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

So I look for a solution and find: SyntaxError: Cannot use import statement outside a module

Which tells me to:

Add "type": "module" to the nearest parent package.json

So add "type": "module" to package.json. The output I get now is:

internal/modules/run_main.js:54
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\Users\010bo\OneDrive\Documents\GitHub\rpg-backend\app\app.ts
[90m    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)[39m
[90m    at Loader.getFormat (internal/modules/esm/loader.js:98:42)[39m
[90m    at Loader.getModuleJob (internal/modules/esm/loader.js:227:31)[39m
[90m    at async Loader.import (internal/modules/esm/loader.js:161:17)[39m {
  code: [32m'ERR_UNKNOWN_FILE_EXTENSION'[39m
}

So again I go online, look for solution and find: Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts

Which tells me to:

Remove "type": "module" from package.json

So I think I've found myself in a catch 22 situation. I'm not sure where to go next.

I also tried the other solution in SyntaxError: Cannot use import statement outside a module

Explicitly name files with the .mjs extension. All other files, such as .js will be interpreted as CommonJS, which is the default if type is not defined in package.json.

This didn't work, either giving me the same error or giving me a different syntax error, I think due to the fact that I am using typescript.

Thank you in advance to anyone who can help.

1 Answer 1

0
const { Request, Response } = require("express")

Same thing for the other ones.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.