Some weird errors show up when using paths for resolve.alias .
See original GitHub issueCan you reproduce the problem with latest npm?
no, I use yarn and it’s the latest
Description
I want to add some common alias in both prod and dev webpack configuration files. To avoid repetitive coding I decided to write them in paths.js, like this:
...
// config after eject: we're in ./config/
module.exports = {
...
servedPath: getServedPath(resolveApp('package.json')),
//common alias
resolvePaths: {
pages: resolveApp('src/pages'),
common: resolveApp('src/common'),
utils: resolveApp('src/utils'),
components: resolveApp('src/components'),
modals: resolveApp('src/components/Modals')
}
};
Then I use it in both configuration files, like this:
resolve: {
...
extensions: ['.js', '.json', '.jsx', ''],
alias: Object.assign(paths.resolvePaths,{
'react-native': 'react-native-web',
})
}
But it doesn’t work and some weird errors show up:
Failed to compile.
Error in ./src/common/less/base.less
Syntax error: Unexpected '}' at line 1 column 16 of the JSON5 data. Still to read: "}"
@ ./src/common/less/base.less 4:14-192 13:2-17:4 14:20-198
However, If I change the part of path.js to:
...
// config after eject: we're in ./config/
module.exports = {
...
servedPath: getServedPath(resolveApp('package.json')),
//common alias
pages: resolveApp('src/pages'),
common: resolveApp('src/common'),
utils: resolveApp('src/utils'),
components: resolveApp('src/components'),
modals: resolveApp('src/components/Modals')
};
And use it in configuration files like this:
resolve: {
...
extensions: ['.js', '.json', '.jsx', ''],
alias: {
'react-native': 'react-native-web',
'common': paths.common,
'pages': paths.pages,
...
})
}
It will work.
Expected behavior
It works correctly.
Environment
Run these commands in the project folder and fill in their results:
npm ls react-scripts
(if you haven’t ejected): have ejectednode -v
: v6.10.2npm -v
: 4.6.1
Then, specify:
- Operating system: macOS 10.12.4
- Browser and version: Chrome 58.0.3029.110 (64-bit)
Thanks.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Webpack doesn't resolve properly my alias - Stack Overflow
I am trying to have a namespace for my app to work as a module, and import my components using this namespace and...
Read more >[Bug] TS doesn't detect webpack aliases? #10088 - GitHub
I have a webpack config with an alias for an import path, VSCode doesn't recognize this, i've tried with the tsconfig.json 's paths...
Read more >Can't resolve path alias : r/reactjs - Reddit
I'm trying to use alias for paths but I can't seem to get it to work I keep getting this error even though...
Read more >Webpack @ alias not working in app - only when running npm ...
For some reason, that I can't seem to figure out, I am getting a swath of errors when deploying a specific app to...
Read more >Webpack Aliases Keep My Code Sane - DEV Community
Specifically with package imports, I wanted to stop dealing with changing all the path locations every time files were refactoring and improved.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
FWIW, if you’re just trying to enable absolute imports like
import MyPage from 'pages/MyPage'
you didn’t need to eject. You can just create an.env
file in your project and addNODE_PATH=src
to it.I’ll try it. Again, thank you for your help.