简体   繁体   中英

getting 'Error: Cannot find module' when importing routes in Express

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

Changing how api.ts is imported did help as @Priyanka suggested. I was still getting Unable to resolve path to module when I did npm run lint . To fully resolve the error I had to change the export syntax in api.ts to export default router . I also had to update eslint config section of my package.json. I followed the suggestions in this StackOverflow post comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM