mysticatea / eslint-plugin-node

Additional ESLint's rules for Node.js
MIT License
958 stars 167 forks source link

Prefer `path.resolve()` to `path.join(__dirname)` #292

Closed fregante closed 2 years ago

fregante commented 2 years ago

It seems to me that path.join(__dirname) is equivalent to path.resolve(), so the latter would be preferred.

Bad

path.join(__dirname, 'dir/my-file.js')

Good

path.resolve('dir/my-file.js')
path.join('just', 'a/relative', '../path', 'my-file.js')
cherryblossom000 commented 2 years ago

path.resolve resolves relative to the current working directory, which is not necessarily the same as __dirname:

If, after processing all given path segments, an absolute path has not yet been generated, the current working directory is used.

fregante commented 2 years ago

Confirmed via

const path = require('path');
console.log(path.resolve(), 'resolve')
console.log(path.join(__dirname), 'join + dirname')