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);
}
};