Next.js: Next.config.js `resolve.modules` not working to resolve module

Created on 1 Feb 2017  路  6Comments  路  Source: vercel/next.js

I'm looking for a simple way to link up a few directories that are in the root of my project to use as paths for webpack to resolve modules. That is, import Button from 'components/Button' without all of the ../.. directory hops. I believe that either of these would do that...

module.exports = config => {
  config.resolve.modules = [
    __dirname,
    'node_modules'
  ]
  return config
}

or with alias...

module.exports = config => {
  config.resolve.alias = {
    components: path.resolve(__dirname, 'components')
  }
  return config
}

Neither of those seem to work. Am I missing something? Is __dirname not giving me my project root?

Most helpful comment

@msell I was able to get it working pretty easily with this .babelrc:

{
  "presets": [
    "next/babel"
  ],
  "plugins": [
    ["module-resolver", {
      "root": ["./"]
    }]
  ]
}

you can try running it yourself and see an example at https://github.com/timberio/next-go

All 6 comments

My apologies. I set up that import incorrectly. It does work on the client when I use the webpack key but not on the server. I guess that's because we don't have webpack on the server. Any other ideas?

I got it working with a Babel plugin instead. https://github.com/tleunen/babel-plugin-module-resolver

It does make me wonder the examples that alias react with Inferno for example, is that using React or Inferno on the server since the alias is just in webpack?

@abstracthat inferno example doesn't use React on the server.
See fjx: https://github.com/zeit/next.js/pull/767

Using https://github.com/tleunen/babel-plugin-module-resolver is the current solution.

Does anybody have any examples of a next.js app working with the babel-plugin-module-resolver? I've been trying for a couple of days to get it to resolve my components using this plugin, but I must be doing something wrong and I haven't seen any examples online.

@msell I was able to get it working pretty easily with this .babelrc:

{
  "presets": [
    "next/babel"
  ],
  "plugins": [
    ["module-resolver", {
      "root": ["./"]
    }]
  ]
}

you can try running it yourself and see an example at https://github.com/timberio/next-go

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jesselee34 picture jesselee34  路  3Comments

kenji4569 picture kenji4569  路  3Comments

renatorib picture renatorib  路  3Comments

olifante picture olifante  路  3Comments

rauchg picture rauchg  路  3Comments