Trying To Setup Top-Level Await In New Typescript Project. Wont Rec

Viewed 36

I am brand new to Typscript and I am simply trying to test this script

// ./src/indes.ts

import fetch from 'node-fetch';

const getResp = await fetch('https://jsonplaceholder.typicode.com/todos/1');\

Yet I keep getting this error:

nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): '.\**\*.ts'
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node src/index.ts`
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
    at new NodeError (internal/errors.js:322:7)
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15)
    at Loader.getFormat (internal/modules/esm/loader.js:105:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:243:31)
    at async Loader.import (internal/modules/esm/loader.js:177:17)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
    at async handleMainPromise (internal/modules/run_main.js:59:12) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'

The goal is to simply write some TypeScript, let it autocompile to the build folder, and then test it.

Im aware that Nodemon is pointing to the Typescript file instead of building but Im just trying to give this script a spin

Im stuck and do not know what to do! I think Ive copied 50 different answers just to see what sticks but I only get errors.

Im using Windows 11 on Node 14.19.0 wit

package.json

{
  "name": "heroku-stack-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "dev": "nodemon --watch './**/*.ts' --exec \"ts-node\" src/index.ts",
  },
  "devDependencies": {
    "@types/node": "^14.18.22",
    "gts": "^3.1.0",
    "nodemon": "^2.0.19",
    "ts-node": "^10.9.1",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "cheerio": "^1.0.0-rc.12",
    "node-fetch": "^3.2.8"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true,
    "exactOptionalPropertyTypes": true,
    "isolatedModules": true,
    "lib": [
      "esnext"
    ],
    "module": "esnext",
    "moduleResolution": "node",
    "noUncheckedIndexedAccess": true,
    "outDir": "dist",
    "strict": true,
    "target": "esnext",
    "useUnknownInCatchVariables": true
  },
  "include": [
    "./src/**/*"
  ]
}

nodemon.json

{
  "watch": "src/**/*.ts",
  "execMap": {
    "ts": "ts-node"
  }
}
0 Answers
Related