I have an ExpressJS app configured to use Typescript. I'm trying to add some routes. When I start the app I keep getting 'Error: Cannot find module 'routes/api'.
Here is my server.ts:
import express from "express";
import * as api from "routes/api";
const app = express(),
port = 3000;
// api = require("routes/api");
app.use("/", api.router);
app.listen(port);
Here is my routes/api.ts (updated):
import express from "express";
import { gql } from "@apollo/client";
const router = express.Router(),
client = require("../apollo");
router.post("/graphql/:owner/:name", (req, res) => {
client
.query({
query: gql`
query($owner: String!, $name: String!) {
repository(name: $name, owner: $owner) {
latestRelease {
name
isLatest
createdAt
publishedAt
updatedAt
url
tagName
resourcePath
}
}
}
`
})
.then((result) =>
console.log(`GraphQL response: ${JSON.stringify(result)}`)
);
});
export default router;
My eslint config is in my package.json. I tried adding import/extensions: 0
to the rules section as mentioned in other StackOverflow posts and adding plugin:import/typescript
under the extends section. Nothing has worked.
Here's the eslint Config (updated) from my package.json:
"eslintConfig": {
"settings": {
"import/resolver": {
"node": {
"extensions": [
".js",
".ts"
]
}
}
},
"parser": "@typescript-eslint/parser",
"extends": [
"airbnb-base",
"prettier",
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": [
"prettier",
"@typescript-eslint"
],
"env": {
"es6": true,
"browser": true
},
"rules": {
"brace-style": [
"error",
"stroustrup"
],
"comma-dangle": [
"error",
"never"
],
"no-unused-vars": [
"warn"
],
"no-var": [
"error"
],
"one-var": [
"error",
"always"
],
"prettier/prettier": [
"error"
],
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never",
"jsx": "never",
"ts": "never",
"tsx": "never"
}
]
}
},
"prettier": {
"trailingComma": "none",
"arrowParens": "always",
"jsxBracketSameLine": false
}
I've also tried changing how router is exported. I've tried module.exports = router
,export default router
and api = require("routes/api"
with no success. I don't understand why I'm getting the error when the file path is obviously correct. I'm still relatively new to Express. Any help would be appreciated.
Update:
Here is my folder structure:
-package.json
-node_modules
-server.ts
-apollo.ts
-routes
-- api.ts