next.js: Next.js 15 import alias not working with turbopack
Link to the code that reproduces this issue
https://github.com/aelassas/next-turbopack
To Reproduce
Create a Next.js 15 project with an internal package package1:
| - my-app/
| - packages/package1/
Add the alias in tsconfig.json:
{
"compilerOptions": {
...
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"],
":package1": ["../packages/package1"],
},
}
}
Import :package1 in a page or component:
import * as package1 from ':package1'
...
Run the dev server with turbopack.
Current vs. Expected behavior
When I run the dev server without turbopack, it works fine. But when I try with turbopack I always get this error:
Module not found: Can't resolve ':package1'
Import map: aliased to relative "../packages/package1" inside of [project]/
Provide environment information
Platform: win32
Arch: x64
Version: Windows 11 Pro
Available memory (MB): 32593
Available CPU cores: 12
Binaries:
Node: 20.17.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant Packages:
next: 15.0.2-canary.7 // Latest available version is detected (15.0.2-canary.7).
eslint-config-next: N/A
react: 19.0.0-rc-1631855f-20241023
react-dom: 19.0.0-rc-1631855f-20241023
typescript: 5.3.3
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Turbopack
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 15 (1 by maintainers)
Turbopack has stricter filesystem access than webpack by default to ensure that files outside of the project directory are not leaking into the compilation which causes the caches to be invalidated too often (i.e. when you deploy a part of the work would always have to be repeated).
We automatically detect the root by looking for a lockfile, in case of your reproduction there is no lockfile so it uses the default of the original project directory. Relevant code: https://github.com/vercel/next.js/blob/1ce3913ef13a22c03d795df7cc8eec2cf0a7ee37/packages/next/src/lib/find-root.ts#L6
We should add a clearer error for these resolving failures to highlight the reason it can’t resolve is the path going out of the project root.
@dualdetail After playing with turbo config, I found a workaround:
Setting
rootto..fixed the issue. But as I said in my previous comment, it should work withtsconfig.jsonwithout having to tweak turbo config.Anyway, thanks for your hint.
From the point of view of a working webpack configuration, this turbopack behaviour represents a regression. Willing to accept that our webpack approach is wrong, but it seems incorrect to have to maintain two contradicting configs for the two sets of tooling.
In our monorepo, modules within the repository are resolved directly to their typescript source in the module’s
src/index.tswith no build step. In most tooling this is simply the--conditionflag, or acustomConditionconfig but we found explicit aliasing to absolute module path to work for next webpack.However, attempting the same within the dev task with turbo enabled results in errors logged like…
I think server relative means it settles to
./Users/user/workspace/monorepo-root/modules/my-module/srcwhich is surely a contradiction that makes it impossible to migrate to turbopack tooling given an absolute path seemed to be needed by webpack.Here’s our (anonymised) source which works fine for webpack and chokes on turbopack.
You can lookout for my “portfolio” repo you get it there next config, how i handled alias in turbopack, beside that i use webpack more but still its working with turbopack. @arodik
next.config.ts
@dualdetail I have tried with the following config before submiting this issue but got the same error:
I think it should work with
tsconfig.jsonwithout having to tweak turbo config.I’ve fixed it. We had
rootDirandbaseUrlpointed tosrcdirectory. Something like this:I’ve simplified the config using the fresh nextjs project as a reference and now we have something like this and it works perfectly:
Hope this will help people with similar problems 😃
I use 15.3.3 - it doesn’t work for me and the error is unclear 😦
I also have Same issue when i using turbopack but i have more Alias so i directly imported from
tsconfig.jsonalso i havve imageloader issue on turbopack