next.js: Module not found: Can't resolve webpack alias

Bug report

Describe the bug

I experience a problem with webpack aliases. They are not working since I’ve updated to Next v9. I get the following error:

Module not found: Can't resolve '~/components/nav' in '/Users/jerrygreen/projects/next-test/pages'

To Reproduce

I’ve made a repo with reproducible bug:

https://github.com/JerryGreen/next.js-issue8452

Steps to reproduce:

git clone https://github.com/JerryGreen/next.js-issue8452
cd next.js-issue8452
yarn
open http://localhost:3000
yarn dev

Expected behavior

The repo I’ve linked should work without bugs

Screenshots

image

System information

  • OS: macos 10.14.5
  • Version of Next.js: 9.0.4

Additional context

I think this might be related to this problem:

https://github.com/zeit/next.js/issues/8157

I am not sure but I’m experiencing this bug too, in my own project, since I’ve updated to Next v9. However, it’s not covered by my reproducible repo, nor by this issue. Just may be related.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 6
  • Comments: 18 (14 by maintainers)

Most upvoted comments

@timneutkens I also have a similar problem to @JerryGreen but I’m using typescript. I have tried to get alias working four the last couple hours but haven’t had much luck.

const config = {
  webpack(config) {
    config.resolve.alias['@modules'] = resolve(
      __dirname,
      'modules'
    )

    return config
  }
}

module.export =  config

I’m getting Module not found: Can't resolve '@modules/bootstrapApi' in pages/api.ts`. Alias doesn’t also work in the other pages

The provided demo deploys to Now and runs locally for me just fine.

However, to be safe, the alias should be:

config.resolve.alias['~'] = resolve(__dirname, './components')

Closing this as there’s no Next.js bug – this is just standard webpack modification.

You’ll want to resolve('./src'), notice the ./ part.