TypeScript module import compilation errors in the API

26 views Asked by At

I'm encountering difficulties while initiating an API. I'm utilizing a template supplied by a company. Nevertheless, upon launching the application, it displays errors. I suspect that these errors stem from issues with module imports in TypeScript. Regrettably, the provided code is in TypeScript, a language with which I am not yet proficient.

package.json

{
  "name": "standalone-app-backend-node",
  "version": "1.0.0",
  "type": "module",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon --watch 'src/**/*.ts' --watch './.env'",
    "database": "json-server --port 5000 --watch db.json"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^1.4.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.18.2",
    "json-server": "^0.17.3",
    "morgan": "^1.10.0"
  },
  "devDependencies": {
    "@faker-js/faker": "^8.0.2",
    "@types/axios": "^0.14.0",
    "@types/cors": "^2.8.13",
    "@types/dotenv": "^8.2.0",
    "@types/express": "^4.17.17",
    "@types/json-server": "^0.14.4",
    "@types/morgan": "^1.9.4",
    "@types/node": "^20.8.6",
    "nodemon": "^2.0.22",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.2.2"
  }

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "nodenext",
    "outDir": "./dist",
    "allowSyntheticDefaultImports": true,
    "composite": true,
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node16",
    "noEmit": true,
    "baseUrl": ".",
    "paths": {
      "@utils": ["./src/utils"],
      "@features/*": ["./src/features/*"],
      "@config": ["./src/config"],
      "@middlewares": ["./src/middlewares"],
      "@repository": ["./src/repository"]
    }
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

index.ts

import express from "express";
import cors from "cors";
import morgan from "morgan";
// @ts-ignore
import dotenv from "dotenv";
import path from "path";
dotenv.config({
  path: path.resolve(".env"),
});

import { AppRoutes } from "@config";
import {
  beforeCheckClientMiddleware,
  errorHandlingMiddleware,
} from "@middlewares";

const port = process.env.PORT || 7200;
const app = express();

app.use(
  morgan(":method :url :status :res[content-length] - :response-time ms")
);
app.use(cors());
app.use(beforeCheckClientMiddleware);
app.use(AppRoutes);
app.use(errorHandlingMiddleware);
app.listen(port, () => {
  console.log(`Server started on port ${port}`);
});

error

node index.ts
node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\Users\yuriv\OneDrive\Documentos\NuvemShopApp\api\src\index.ts
    at new NodeError (node:internal/errors:371:5)
    at Object.file: (node:internal/modules/esm/get_format:72:15)
    at defaultGetFormat (node:internal/modules/esm/get_format:85:38)
    at defaultLoad (node:internal/modules/esm/load:13:42)
    at ESMLoader.load (node:internal/modules/esm/loader:303:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:230:58)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:244:11)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

I hope the server will run. I've tried changing the module types to Node 16, NextNode, and others, but without success.

0

There are 0 answers