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.

Some weird errors show up when using paths for resolve.alias .

See original GitHub issue

Can 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:

  1. npm ls react-scripts (if you haven’t ejected): have ejected
  2. node -v: v6.10.2
  3. npm -v: 4.6.1

Then, specify:

  1. Operating system: macOS 10.12.4
  2. Browser and version: Chrome 58.0.3029.110 (64-bit)

Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
gaearoncommented, Jun 7, 2017

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 add NODE_PATH=src to it.

0reactions
leadreamcommented, Jun 7, 2017

I’ll try it. Again, thank you for your help.

Read more comments on GitHub >

github_iconTop 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 >

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