Resolving require paths with webpack
I'm still confused how to resolve module paths with webpack. Now I write:
myfile = require('../../mydir/myfile.js')
but I'd like to write
myfile = require('mydir/myfile.js')
I was thinking that resolve.alias may help since I see a similar example using { xyz: "/some/dir" }
as alias then I can require("xyz/file.js")
.
But if I set my alias to { mydir: '/absolute/path/mydir' }
, require('mydir/myfile.js')
won't work.
I feel dumb because I've read the doc many times and I feel I'm missing something.
What is the right way to avoid writing all the relative requires with ../../
etc?
Solution 1:
Webpack >2.0
See wtk's answer.
Webpack 1.0
A more straightforward way to do this would be to use resolve.root.
http://webpack.github.io/docs/configuration.html#resolve-root
resolve.root
The directory (absolute path) that contains your modules. May also be an array of directories. This setting should be used to add individual directories to the search path.
In your case:
webpack config
var path = require('path');
// ...
resolve: {
root: path.resolve('./mydir'),
extensions: ['', '.js']
}
consuming module
require('myfile')
or
require('myfile.js')
see also: http://webpack.github.io/docs/configuration.html#resolve-modulesdirectories
Solution 2:
For future reference, webpack 2 removed everything but modules
as a way to resolve paths. This means root
will not work.
https://gist.github.com/sokra/27b24881210b56bbaff7#resolving-options
The example configuration starts with:
{
modules: [path.resolve(__dirname, "app"), "node_modules"]
// (was split into `root`, `modulesDirectories` and `fallback` in the old options)
Solution 3:
resolve.alias
should work exactly the way you described, so I'm providing this as an answer to help mitigate any confusion that may result from the suggestion in the original question that it does not work.
a resolve configuration like the one below will give you the desired results:
// used to resolve absolute path to project's root directory (where web pack.config.js should be located)
var path = require( 'path' );
...
{
...
resolve: {
// add alias for application code directory
alias:{
mydir: path.resolve( __dirname, 'path', 'to', 'mydir' )
},
extensions: [ '', '.js' ]
}
}
require( 'mydir/myfile.js' )
will work as expected. If it does not, there must be some other issue.
If you have multiple modules that you want to add to the search path, resolve.root
makes sense, but if you just want to be able to reference components within your application code without relative paths, alias
seems to be the most straight-forward and explicit.
An important advantage of alias
is that it gives you the opportunity to namespace your require
s which can add clarity to your code; just like it is easy to see from other require
s what module is being referenced, alias
allows you to write descriptive require
s that make it obvious you're requiring internal modules, e.g. require( 'my-project/component' )
. resolve.root
just plops you into the desired directory without giving you the opportunity to namespace it further.