I have an ExpressJS application set up with Typescript. I am currently facing an issue while trying to add routes. Upon starting the app, I continuously receive the error message 'Error: Cannot find module 'routes/api'.
Below is my server.ts file:
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);
And here is my updated routes/api.ts file:
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;
I have setup my eslint config in package.json but so far, adding import/extensions: 0
to the rules section and including plugin:import/typescript
under the extends section did not resolve the issue. Here's the updated eslint Config from my package.json:
"eslintConfig": {
...
In addition, I have tried different export methods for router such as module.exports = router
, export default router
, and
api = require("routes/api"
without any success. Despite the correct file path being used, I continue to face this error. As I am new to Express, any assistance or guidance on this matter would be highly appreciated.
Update:
Here is my folder structure:
-package.json
-node_modules
-server.ts
-apollo.ts
-routes
-- api.ts