【问题标题】:Typescript Node MODULE_NOT_FOUND打字稿节点 MODULE_NOT_FOUND
【发布时间】:2021-12-05 18:57:11
【问题描述】:

当我运行yarn start (ts-node src/index.ts) 时,我得到MODULE_NOT_FOUND Error: Cannot find module 'src/isAuth'。在我添加新文件 isAuth.ts 之前它可以正常工作

看看我的文件是如何组织的。

这是我的.tsconfig

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
    "declaration": false,

    "composite": false,
    "removeComments": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "esModuleInterop": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "baseUrl": ".",
    "rootDir": "src"
  },
  "exclude": ["node_modules"],
  "include": ["./src/**/*.tsx", "./src/**/*.ts", "src"],
 
}

这是isAuth.ts 文件:

import { verify } from "jsonwebtoken";
import { MiddlewareFn } from "type-graphql";
import { MyContext } from "./MyContext";
import * as dotenv from 'dotenv';
dotenv.config();


export const isAuth: MiddlewareFn<MyContext> = ({context}, next) => {
    const authorization = context.req.headers['authorization']

    if (!authorization) {
        throw Error('Not authenticated')
    }

    try {
        const token = authorization.split(' ')[1]
        const payload = verify(token, process.env.ACCESS_TOKEN_SECRET!)
        context.payload = payload as any
    } catch (error) {
        throw Error('Not Authenticated')
    }
    return next()
}

person.resolver.ts内部使用。

请注意,MyContext.ts 没有这个问题,它是一个接口。是 const 函数有问题,还是有其他问题?

【问题讨论】:

  • src/isAuth - 在您的文件/文件夹结构中,isAuth.ts 文件似乎放在 services 文件夹中。嗯?
  • 不,它不在/services
  • 您能否显示isAuth.ts 以及它在哪里使用/导入?出口了吗?
  • 我编辑了问题
  • 只是一个疯狂的猜测:您是否尝试过在没有 dotenv 的情况下运行服务器 - 您是否尝试过将 import 和 dotenv.config() 放在 cmets 下会发生什么?我不知道MiddlewareFn 从未使用过它。但也许可以尝试某种消除过程,并尝试确定哪个导入产生问题。这就是我会尝试的

标签: node.js typescript tsconfig


【解决方案1】:

我是这个包管理的新手,但我认为你应该删除 node_modules 文件夹然后运行 ​​yarn add 然后 yarn start

【讨论】:

  • 我之前试过了,还是不行
猜你喜欢
  • 2018-04-15
  • 2018-04-10
  • 2013-02-25
  • 2022-01-14
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 2019-03-03
相关资源
最近更新 更多