Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use path aliases #334

Closed
muco-rolle opened this issue Jun 20, 2021 · 14 comments
Closed

How to use path aliases #334

muco-rolle opened this issue Jun 20, 2021 · 14 comments

Comments

@muco-rolle
Copy link

muco-rolle commented Jun 20, 2021

I would like to use absolute path aliases but couldn't figure out how to set it up with this project

I would like a path like this:

import { formatNumber } from "../../../utils"

be like this:

import { formatNumber } from "@utils"

@muco-rolle muco-rolle changed the title Using path aliases How to use path aliases Jun 20, 2021
@egoist
Copy link
Owner

egoist commented Jun 26, 2021

using "paths" in tsconfig.json should work

@hampuskraft
Copy link

hampuskraft commented Sep 16, 2021

@egoist I get Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@app/constants' using tsup with path aliases. I'll gladly provide a reproducible example if necessary, but you can try it out with a minimal tsconfig.json, adding something like:

{
  "compilerOptions": {
    "paths": {
      "@app/*": ["./src/*"]
    }
  }
}

@egoist
Copy link
Owner

egoist commented Sep 16, 2021

@hampuskraft maybe you forgot "baseUrl": "." in compilerOptions.

@hampuskraft
Copy link

That gives the same result, since baseUrl isn't required for relative paths (see microsoft/TypeScript#40101).

@egoist
Copy link
Owner

egoist commented Sep 16, 2021

@hampuskraft yeah, a repro would be nice then, since I can't reproduce it locally

@hampuskraft
Copy link

@egoist Here's a repro, thanks! https://github.com/hampuskraft/tsup-paths

@egoist
Copy link
Owner

egoist commented Sep 16, 2021

@hampuskraft I tried npx tsup src/module-b.ts --clean --format esm --sourcemap --target esnext and it works fine.

CleanShot 2021-09-16 at 23 29 19@2x

@egoist
Copy link
Owner

egoist commented Sep 16, 2021

@hampuskraft: Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@app/constants'

I guess this is a runtime error? seems to be related to #409

@egoist
Copy link
Owner

egoist commented Sep 16, 2021

I'll close this and further updates will be added to #409.

@egoist egoist closed this as completed Sep 16, 2021
@bhvngt
Copy link

bhvngt commented Nov 18, 2021

Hi, it seems that path alias is not working for tsup-node. Since my module is targeted for node, I would prefer to not bundle them using tsup. Is there a workaround for this usecase?

@egoist
Copy link
Owner

egoist commented Nov 18, 2021

@bhvngt does tsup work for you? it should exclude packages specified in dependencies by default

@bhvngt
Copy link

bhvngt commented Nov 18, 2021

I have been using some of node's built-in packages and my project is following esm packaging standards with type set as module. This is generating a bundled up code which throws following error when I import my library from some other package.

Error: Dynamic require of "fs" is not supported.

@egoist
Copy link
Owner

egoist commented Nov 18, 2021

@bhvngt a repo would be helpful

@bhvngt
Copy link

bhvngt commented Nov 18, 2021

@egoist I have created a repo tsup-node-esm which replicates my issue.

I have realised that if I move tmp package to dependencies, it all seems to work out fine.

I do see that the code gets inlined during transpilation. Is there anyway to avoid that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants