React. Error when importing an object (Attempted import error)

Standard scheme: there are several files with constant objects in the same folder, sent for export to index.js for further general use:

The userConstants file.js

export const userConstants = {};

File itemConstants.js

export const itemConstants = {};

File index.js (I use absolute paths)

export * from 'constants/userConstants';
export * from 'constants/itemConstants';

I need to get the constants of only one object, I'm trying to destruct them in the component file:

import { userConstants } from "constants";

I get an error: Attempted import error: 'userConstants' is not exported from 'constants'.

Without destructuring, there is no error. Also, if you combine into 1 object and export by default, the error is also in the component file, where I try to destruct and get a certain constant object.

What could be the problem? This scheme was used in other projects, but relative paths were used there. Maybe because of this, webpack can't build the files correctly?

Author: Dominik, 2020-02-13

1 answers

Webpack does not correctly perceive the folder name "constants"in the paths. If you call "_constants" the error will be fixed.

 0
Author: Dominik, 2020-02-13 10:42:21