'Typescript Node MODULE_NOT_FOUND
When I run yarn start
(ts-node src/index.ts), I get MODULE_NOT_FOUND
Error: Cannot find module 'src/isAuth'
. It use to work before I added that new file isAuth.ts
Here'a look of how my file is organized.
Here's my .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"],
}
this is the isAuth.ts
file:
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()
}
It's being used inside person.resolver.ts
.
Notice I don't have this problem with MyContext.ts
which is an interface. Are const function a problem, or is it something else?
Solution 1:[1]
Try renaming the exported function in the isAuth.ts to something else, and since it maybe that it is in another folder, try dragging and drop it on top of the folder named 'src', also as stated by Stefan Zivkovic in comments, show the file where it is imported, might have broken import path and lastly, just asking, was it resolved? If so, please let us know, thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |