1. ./xx
Relative path issues in file operations
Check out the following scenarios:
# /js/foo/a.txt
hello World
# /js/foo/index.js
const fs = require('fs');
fs.readFile('./a.txt',(err,data) => {
if (err) {
console.log('error');
} else {
console.log(data.toString());
}
})
When /js
executed in the directory node foo/index.js
, the file cannot be found. The reason: /js/foo/index.js
the relative path of reading the file in the file is:, ./a.txt
and this relative path is actually relative to node
the path where the command is executed , that is, the above execution In the case of node, the search path for file operations is: js/a.txt
Obviously /js
, there is no such file in the directory, and the search fails.
Look at another scene
# /js/foo/a.txt
hello World
# /js/foo/index.js
const fs = require('fs');
fs.readFile('./a.txt',(err,data) => {
if (err) {
console.log('error');
} else {
console.log(data.toString());
}
})
# /js/other.js
require('./foo/index'); // 引入/foo目录下的index.js文件
/js
Execute the node other.js
command in the directory , the execution result is:
Can't find the file
The reason the same as before, although the js
execution of the command, but still in the introduction and implementation of the document index.js
file, because the node
directory command is: /js
catalog, so when the file operation, look for the file directory is: /js/a.txt
apparently they Is not found
Therefore, in file operations, the relative path is unreliable. To solve this problem, you need to change the relative path to an absolute path. But if you just change the C:\node\js\foo\a.txt
path of file operations to the path, when you deliver the project, you also need to change the path to the absolute path of the computer where the current project is located. Obviously this is not feasible, so it comes __dirname
into play.
And what __dirname
is it?
In each module, in addition require
, exports
other relevant API module, there are two special member
__dirname
Get the directory where the current file is located (absolute path)__filename
Get the directory where the current file is located, including the current file (absolute path)__dirname
The sum__filename
is not affected by the path to which the node command is executed- The above two ways to obtain the path are obtained dynamically
Since it is __dirname
not affected by the path to which the node command belongs, and the absolute path of the current file can be dynamically obtained at the same time, it can be a good choice. /foo/index.js
Modify:
# /js/foo/index.js
const fs = require('fs');
const path = require('path');
// 采用path.join()对于拼接的路径自动进行修复,避免不必要的失误操作造成的文件访问不到的问题
fs.readFile(path.join(__dirname + './a.txt'),(err,data) => {
if (err) {
console.log('error');
} else {
console.log(data.toString());
}
})
2. require()
Path problem in
require
The path written in the module has nothing to do with the path of the file operation. The path is relative to the file module, that is, the relative path relative to the directory where the current file module (file) is located.
# /js/other.js
require('./foo/index.js');
# /js/foo/index.js
console.log('1');
At this time, the search ./foo/index.js
is relative to the /js
directory