Talking about the relative path problem in nodejs file operation

1. ./xxRelative 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 /jsexecuted in the directory node foo/index.js, the file cannot be found. The reason: /js/foo/index.jsthe relative path of reading the file in the file is:, ./a.txtand this relative path is actually relative to nodethe 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.txtObviously /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文件

/jsExecute the node other.jscommand in the directory , the execution result is:

Can't find the file

The reason the same as before, although the jsexecution of the command, but still in the introduction and implementation of the document index.jsfile, because the nodedirectory command is: /jscatalog, so when the file operation, look for the file directory is: /js/a.txtapparently 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.txtpath 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 __dirnameinto play.
And what __dirnameis it?
In each module, in addition require, exportsother relevant API module, there are two special member

  • __dirnameGet the directory where the current file is located (absolute path)
  • __filenameGet the directory where the current file is located, including the current file (absolute path)
  • __dirnameThe sum __filenameis 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 __dirnamenot 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.jsModify:

# /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

requireThe 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.jsis relative to the /jsdirectory

Guess you like

Origin blog.csdn.net/chen__cheng/article/details/115032006