JS: How to Import in Raw Source Code and Webpack the Same Way
September 12, 2023•120 words
Relative JS imports in a file in a deep folder structure is crazy with double-dots: ../
However, browser will allow only: / or ./ or ../
Webpack can't understand ./ nor ../ because it runs in a current working dir, not resolving using hostname and URL the way browser does.
There's 1 option to fit both browser & Webpack: The root slash
- Always use root slash / in import so browser will resolve from root after hostname
- Webpack consider
/
as the root of file system- However, if a symbolic link 'foobar' is created in working dir of Webpack
- Webpack will resolve
/foobar
according to the symbolic link instead of from root dir of file system.