Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to configure a webpack alias #877

Closed
thisbejim opened this issue Jan 25, 2017 · 12 comments
Closed

How to configure a webpack alias #877

thisbejim opened this issue Jan 25, 2017 · 12 comments

Comments

@thisbejim
Copy link

thisbejim commented Jan 25, 2017

I'd like to configure some webpack aliases to point to some of my folders (not to a lib in node_modules).

Here is a simple example:

var path = require('path');

module.exports = {
  webpack: function (config) {
    config.resolve.alias = {
      'components': path.resolve('./components'),
    };
    return config
  }
}

However, if I try to import a component in a page e.g:

import { NavBar } from 'components';

I get the following error:Error: Cannot find module 'components'

Any idea how to get next to play nice with an alias like this?

@arunoda
Copy link
Contributor

arunoda commented Jan 25, 2017

This is bit tricky and you need more support from us because or SSR.
We've a ongoing PR here: #767 which add a alias functionality.

@artdevgame
Copy link
Contributor

I'm looking for the same answer. I've checked out your suggestion @arunoda, but it didn't seem to resolve the problem.

@arunoda
Copy link
Contributor

arunoda commented May 7, 2017

Now we do this like this:

  • For the client side, we can always use webpack alias.
  • But for the server side, we can use module-alias npm package as we use here.

@arunoda arunoda closed this as completed May 7, 2017
@benjaminjackman
Copy link

benjaminjackman commented May 21, 2017

@arunoda my use case is a bit closer to @thisbejim than the example used there. Rather than override certain things in production or development, i just want to be able refer to my components with

import foo from "components/some-component"

rather than some combination of

../../../components/components

is the suggested way to do that still to create a server.js file but then drop the if(!dev) line?

EDIT: It doesn't seem to work i still get

[1] { Error: Cannot find module 'components/common-boot'
[1]     at Function.Module._resolveFilename (module.js:470:15)
[1]     at Function.Module._load (module.js:418:25)
[1]     at Module.require (module.js:498:17)
[1]     at require (internal/module.js:20:19)
[1]     at Object.<anonymous> (XXXX/.next/dist/pages/mimu/piano.js:43:1)
[1]     at Module._compile (module.js:571:32)
[1]     at Object.Module._extensions..js (module.js:580:10)
[1]     at Module.load (module.js:488:32)
[1]     at tryModuleLoad (module.js:447:12)
[1]     at Function.Module._load (module.js:439:3) code: 'MODULE_NOT_FOUND' }

``

@ghost
Copy link

ghost commented Jul 13, 2017

The best option is to go with babel-plugin-module-alias

@zentuit
Copy link

zentuit commented Aug 10, 2017

Has anyone gotten this to work? I cannot get this aliasing stuff to work so I can eliminate the horrible paths in the imports, ie
import { someFunction } from 'lib/util'
rather than
import { someFunction } from '../../lib/util'

I have tried babel-plugin-module-alias, babel-plugin-webpack-alias, setting webpack resolve.modules all to no avail.

@thisbejim
Copy link
Author

I've been using module-alias successfully, just make sure you add the changes from their documentation to your .babelrc file.

@zentuit
Copy link

zentuit commented Aug 11, 2017

Found my issue, everything is working great now. Thanks!

@timneutkens
Copy link
Member

There's an example for doing this now https://github.com/zeit/next.js/tree/master/examples/with-absolute-imports

@zentuit
Copy link

zentuit commented Sep 8, 2017

I'm sorry, I should have made this example when I got mine working.

@timneutkens
Copy link
Member

@zentuit no worries 😄 👍

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
@arunoda @benjaminjackman @artdevgame @zentuit @timneutkens @thisbejim and others