Create-react-app: Some weird errors show up when using paths for resolve.alias .

Created on 7 Jun 2017  路  5Comments  路  Source: facebook/create-react-app

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鈥檛 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.

question

Most helpful comment

FWIW, if you鈥檙e just trying to enable absolute imports like import MyPage from 'pages/MyPage' you didn鈥檛 need to eject. You can just create an .env file in your project and add NODE_PATH=src to it.

All 5 comments

Since you have ejected, we encourage you to ask for help in channels specific to underlying projects (in this case, webpack). We only know our own configuration well, and we can鈥檛 really provide support for every possible change in the configuration. I鈥檒l close this as it鈥檚 not very actionable for us, but maybe somebody who knows webpack well can help you. Cheers!

Please open this issue on Stack Overflow or similar help community.
Unfortunately, we are too busy to provide support for ejected setups. Sorry!

This seems like a malformed webpack loader configuration or invalid syntax in your LESS.
Please be mindful to read all the comments we have around the loader section.
If something's not clear, please let us know or send a PR improving the documentation. Thanks!

@gaearon @Timer ok, I'll ask some other places. thanks for your reply.

FWIW, if you鈥檙e just trying to enable absolute imports like import MyPage from 'pages/MyPage' you didn鈥檛 need to eject. You can just create an .env file in your project and add NODE_PATH=src to it.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

onelson picture onelson  路  3Comments

Evan-GK picture Evan-GK  路  3Comments

barcher picture barcher  路  3Comments

stopachka picture stopachka  路  3Comments

adrice727 picture adrice727  路  3Comments