path.join vs path.resolve with __dirname

前端 未结 3 1839
难免孤独
难免孤独 2020-12-04 08:11

Is there a difference when using both path.join and path.resolve with __dirname for resolving absolute path in Node.js?

3条回答
  •  半阙折子戏
    2020-12-04 08:36

    from doc for path.resolve:

    The resulting path is normalized and trailing slashes are removed unless the path is resolved to the root directory.

    But path.join keeps trailing slashes

    So

    __dirname = '/';
    path.join(__dirname, 'foo/'); // '/foo/'
    path.resolve(__dirname, 'foo/'); // '/foo'
    

提交回复
热议问题