Assistant Root Path Import Assistant

In real life, because I need to import a helper or component, I have this problem

import approxPerDay from '../../../utils/approxPerDay.js'
import otherstuff from '../components/otherstuff'

and in another file it could be import approxPerDay from '../utils/approxPerDay.js'

It is very complicated and time consuming; it is a relative path. Is there any npm or helper that can solve this problem?

+4
source share
1 answer

It depends on your module. For Webpack 2you can do something like this:

module.exports = {
    ...

    resolve: {
        modules: [
            'node_modules',
            path.resolve(__dirname + '/src')
        ],
        alias: {
            src: path.resolve(__dirname + '/src')
        }
    },

    ...
}

same for Webpack 1:

module.exports = {
    ...

    resolve: {
        root: [
            path.resolve(__dirname  + '/src')
        ],
        alias: {
            src: path.resolve(__dirname  + '/src')
        }
    },

    ...
}

What you can use srcas a native path as follows:

import approxPerDay from 'src/utils/approxPerDay.js'
import otherstuff from '../components/otherstuff'
+4
source

Source: https://habr.com/ru/post/1677515/


All Articles