Closed osdevisnot closed 9 years ago
In general, ./ refers to the directory on which the node command was called. It does not refer to the directory of the file being executed, whereas dirname refers to the directory where the file being executed resides. However ./ behaves the same as dirname for require, so there is not really a reason, other than convention, why it could not be used instead. In early versions of node.js, best practice encouraged the use of path.join rather than hardcoding '/' (forward slash) in the path string to ensure compatibility with Windows, as Windows uses '\' (backslash), but the Windows version of Node.js now handles backslashes effectively, so again, it becomes just a matter of convention (unless you are passing path arguments to another Windows program.) I can't think of a reason, technically speaking, that your example code would not work just as well in the use of require. That might not be the case in other instances of working with a path however, so I tend to adhere to a convention that helps me to be mindful of that fact.
Here is a good reason for not using those requires : they break Webpack.
WARNING in ./~/node-dir/index.js
Critical dependencies:
11:20-69 the request of a dependency is an expression
12:26-81 the request of a dependency is an expression
@ ./~/node-dir/index.js 11:20-69 12:26-81
Using the more usual requires in index.js fixes the issue :
exports.readFiles = require('./lib/readfiles');
exports.readFilesStream = require('./lib/readfilesstream');
I'd report that issue to web pack as the code is valid and should not break it.
Right, I would report it to webpack. Btw, this style of require is not supported with browserify and ES6 modules.
I have simplified the require statements in index.js to use non-dynamic require statements.
I see index uses dynamic requires here, here and here. I could very well write same code like so:
Is there any specific reason for doing so? What purpose does it serve?
NB: Motivation for this question: trying to understand how module loaders work.