3

I've tried many things to solve this issue. I'm trying to import from absolute paths defined on my tsconfig.json

on a file on my project I'm trying to use import { UserModel } from "model/User";

And on my tsconfig.json I have

{
....
"baseUrl": "src", 
"paths": {
  "model/*": ["model/*"]`
  }
....
}

I've tried changing the baseUrl to ".", "./" and changing the paths as well, but I'm always getting the same error:

Error: Cannot find module 'model/User'

I have also tried to add an index.ts file to the model folder and export models from there, but still.. same issue. Any help is appreciated.

1 Answer 1

1

If you are writing a package, then probably you are reserved to using the ttypescript lib + @zerollup/ts-transform-paths plugin - those will do the job. Also, building your package will have to use ttsc instead of tsc.

If, on the other hand, you are using a bundler to bundle your code together (like webpack), you will have to reserve to the ways provided by the bunder's config.

Also, if you are using ESLint, you'll have to make sure it uses tsconfig.json via eslint-import-resolver-typescript npm package.

Also, if you are using Jest for unit-tests, you will have to enumerate the possible "roots" like this:

  moduleDirectories: ['node_modules', 'src'],
Sign up to request clarification or add additional context in comments.

3 Comments

Hey man thanks for your answer. I installed the plugin and I got 4 vulnerabilities, 2 being of high importance. Don't you know of another way maybe?
Hey hey! Well, this one should be server-only build-time tool, so I actually did not dig into that "vulnerability" topic. Back in a day it was the simplest way for me to make it work, and I still keep using this approach.
Cool! Thanks for the help man, appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.