This question already has an answer here:
-
What does “./” (dot slash) refer to in terms of an HTML file path location?
9 answers
I have been reading through RequireJS
i dont get the difference between
define(["./cart", "./inventory"], function(cart, inventory) {...}
and
define(["cart", "inventory"], function(cart, inventory) {...}
what difference does the "./" makes?? and what alternative do we have to do , to use dependency paths without "./" ?
Generally speaking, the presence of .
, ..
and /
in module names in RequireJS has the same meaning as for HTTP paths.
The presence of ./
in a dependency tells RequireJS to start its search from the current directory. It is extremely useful if what you want is use a module which you know is located in the same directory as the one that needs it. Let's say moduleA needs moduleB and both are in the same directory then moduleA can just require "./moduleB"
. One advantage of doing this is that you then don't have to worry about where the modules are stored. If you move them around but they are always together in the same directory then moduleA will always find moduleB.
Another thing is that if you require("moduleX")
and there are multiple modules named moduleX available, which module are you going to get? Relative paths clarify which one you are going to get.
You ask how you can get rid of the ./
First, I'd ask do you really want to get rid of it? In the scenario I gave where two modules are in the same directory and this relation is meant to be stable, then it does not make sense to get rid of ./
. It actually helps people reading the code. If what you are requiring is something like jQuery, which happens to be located in the same directory as another module at some moment but could legitimately be installed somewhere else, and which you would conceivably want to be unique for a given application, then it makes sense to have it be at a global well-known location. To do this, you use paths
in your requirejs configuration, like:
paths: {
'jquery': 'external/jquery-1.9.1'
}
This is an actual piece of code part of an actual application. No matter which module wants jquery, it just issues require("jquery")
. It does not have to worry about were jquery is located relative to itself. You can specify a path like this for any module you like, not just jQuery. If jQuery moves to a different location in the future or you want to serve it from a CDN, then this mapping in the snippet above is edited, but require("jquery")
still works.
In context of your code, it means define cart in same directory as the current executing script.
The alternative is to use absolute paths:
/var/www/site/cart
as:
define(["/var/www/site/cart", "/var/www/site/inventory"], function(cart, inventory) {...}
Wikipedia
define(["./cart", "./inventory"], function(cart, inventory) {...}
it search specific file in current directory in filesystem
define(["cart", "inventory"], function(cart, inventory) {...}
it search specific file in root directory in filesystem
./
is refers to the current directory, to get the module currently in the same directory as the script. No ./
refers to the node path, where native and installed modules go (e.g.: net
, fs
, and anything use install via npm
) Using an absolute path can get anywhere in the filesystem (/i/like/node/js/modules.js
)
'/' refers to the root directory and
'./' refers to the current directory
define(["./cart", "./inventory"]
: This will refer cart in the current directory where script is being executed.
./ means that you'll stay in the same directory.