0

I'm getting this error Cannot use Import outside of a module, I've tried adding "type": "module" to package.json but I only get "Unknown file extension .ts". I've installed ts-node and I'm not sure why this is happening considering I have the same tsconfig.server.json and tsconfig.json from another running project.

I'm pretty new to nodejs and I would love for any directions or leads or suggestions thanks!

Here's my tsconfig.json

{
    "compilerOptions": {
      "target": "es5",
      "lib": ["dom", "dom.iterable", "esnext"],
      "allowJs": true,
      "skipLibCheck": true,
      "strict": true,
      "forceConsistentCasingInFileNames": true,
      "noEmit": true,
      "esModuleInterop": true,
      "module": "esnext",
      "moduleResolution": "node",
      "resolveJsonModule": true,
      "isolatedModules": false,
      "jsx": "preserve"
    },
    "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/**/*.ts"],
    "exclude": ["node_modules"]
  }

Here's my tsconfig.server.json

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
      "module": "commonjs",
      "outDir": "dist",
      "target": "es2020",
      "isolatedModules": false,
      "noEmit": false
    },
    "include": ["*.ts"]
  }

Here's my package.json

{
  "name": "koa-pg",
  "version": "1.0.0",
  "description": "Set up of Koa.js & PostgresSQL",
  "main": "index.js",
  "scripts": {
    "start": "NODE_PATH=src nodemon src/index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "Test",
    "Node",
    "Koa",
    "PostgresSQL"
  ],
  "author": "Keith Carrillo",
  "license": "ISC",
  "dependencies": {
    "@koa/router": "^10.1.1",
    "@types/koa": "^2.13.4",
    "@types/koa__router": "^8.0.7",
    "@types/koa-morgan": "^1.0.5",
    "@types/koa-response-time": "^2.1.1",
    "@types/koa-router": "^7.4.4",
    "dotenv": "^10.0.0",
    "enforce-node-path": "^1.0.0",
    "isomorphic-fetch": "^3.0.0",
    "koa": "^2.13.1",
    "koa-morgan": "^1.0.1",
    "koa-response-time": "^2.1.0",
    "nodemon": "^2.0.12",
    "ts-node": "^10.2.1",
    "ts-node-dev": "^1.1.8",
    "typescript": "^4.4.2"
  }
}

Here's my app.ts

import router from "../routing";
import Koa from "koa";
import ResponseTime from "koa-response-time";
import Morgan from "koa-morgan";
/*
 * exports an start module
    assisng asynce function so you can await app.listen(3000)
    catch an error then try display error
 */
const app = new Koa();

app.use(ResponseTime());
app.use(Morgan("combined"));
app.use(router.routes());

exports.start = async function () {
  try {
    app.listen(3000);
  } catch (error) {
    console.log(error);
  }
};
justkeithcarr
  • 1,027
  • 1
  • 7
  • 13
  • Can you try const Koa = require("koa")l for example? – Shai Sep 10 '21 at 20:53
  • Hi @Shai thanks for your suggestion, I just tried replacing imports with require but typescript wants the types and throw it's errors. I first tried replacing router's import statement, but now it's complaining about routes() not existing on router. "Property 'routes' does not exist on type '"../routing"'." – justkeithcarr Sep 10 '21 at 21:11
  • Ahh missed the ts, you might need to change the config or the file extension as stated here https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import – Shai Sep 10 '21 at 21:24

1 Answers1

0

For the current tsconfig.server.json, simply add the nodemon.json file to the root of the project with the following content:

{
  "watch": ["src"],
  "exec": "ts-node --project tsconfig.server.json  ./src/index.ts",
  "ext": "js,json,ts"
}

OR just set TS_NODE_PROJECT env in the package.json:

...
  "scripts": {
    "start": "TS_NODE_PROJECT=tsconfig.server.json NODE_PATH=src nodemon src/index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
...

If you want to use the tsconfig.server.json file to compile ts with support esnext modules:

  1. Remove "module": "commonjs" from the tsconfig.server.json
  2. Add "type": "module" to the package.json
  3. Create nodemon.json with the following content:

{
  "watch": ["src"],
  "exec": "TS_NODE_PROJECT=tsconfig.server.json node --es-module-specifier-resolution=node --loader ts-node/esm ./src/index.ts",
  "ext": "js,json,ts"
}
Denwakeup
  • 256
  • 2
  • 4