question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

with pnpm vite resolve styles @import prefix with ~ error

See original GitHub issue

Describe the bug

with pnpm, vite resolve dependency package styles @import prefix with ~ error

// vite.config.ts
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: [
      {
        find: /^~/,
        replacement: '',
      },
    ],
  },
});
// src/main.tsx
import 'dantdv3/es/style/index.less'
// dantdv3/es/style/index.less
@import '~antd/es/table/style/index.less';
@import '~antd/es/style/themes/default.less';

Reproduction

https://github.com/lastSeries/vite-pnpm-less-resolve

System Info

System:
    OS: macOS 12.3.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 612.50 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.6.1 - ~/.nvm/versions/node/v16.6.1/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 7.20.3 - ~/.nvm/versions/node/v16.6.1/bin/npm
  Browsers:
    Chrome: 100.0.4896.127
    Firefox: 97.0.1
    Safari: 15.4
  npmPackages:
    @vitejs/plugin-react: ^1.3.0 => 1.3.1 
    vite: ^2.9.2 => 2.9.5

Used Package Manager

pnpm

Logs

ENOENT: no such file or directory, open '${workspaceFolder}/antd/es/style/themes/default.less'
5:06:36 PM [vite] Internal server error: ENOENT: no such file or directory, open '${workspaceFolder}/antd/es/style/themes/default.less'
  Plugin: vite:css
  File: ${workspaceFolder}/node_modules/.pnpm/dantdv3@0.3.27_react-dom@18.0.0+react@18.0.0/node_modules/dantdv3/es/style/antd-extension.less
  1  |  @import '~antd/es/table/style/index.less';
     |                                             ^
  2  |  @import '~antd/es/style/themes/default.less';
  3  |

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bluwycommented, Apr 21, 2022

Importing files with @import '~bla' is not supported in Vite and I don’t think aliasing ~ should be a way to fix it. Aliases should (ideally) only be used for your source code. The ~ convention in less is Webpack-specific, and it’s also discouraged now, so I’d recommend contacting the package author to remove the ~ and it should work again. Otherwise sapphi-red’s workaround is the way to go for now. Closing as wontfix.

0reactions
bluwycommented, Apr 21, 2022

The issue is that if you alias ~ to workaround the less import, Vite will import it from /node_modules/antd which wouldn’t work for pnpm like you mentioned. This behaviour is incorrect in the first place because Vite should resolve the import from /node_modules/dantdv3/node_modules/antd instead, but since this is an effect of using the ~ alias affecting node_modules, it’s out of scope for Vite here IMO.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vite Configure Absolut Import Paths Gives Styelint Error
Try removing the type: "module" from package.json file. This may help.
Read more >
Shared Options | Vite
Env variables starting with envPrefix will be exposed to your client source code via import.meta.env. SECURITY NOTES. envPrefix should not be set as...
Read more >
rollup failed to resolve import "react-dom/client" - You.com
16:42:54 [vite] Internal server error: Failed to resolve import "./encode.js" from "node_modules/@libp2p/mplex/src/mplex.ts?v=3146d3a3".
Read more >
Docs • Svelte
Any time you have a reference to a store, you can access its value inside a component by prefixing it with the $...
Read more >
Is Vite currently the best bundler for Svelte? - codechips
They must however be prefixed with VITE_ . Vite support many ways to import your environment variables through different .env file. You can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found