What's the difference between path.resolve and path.join?

前端 未结 3 1226
無奈伤痛
無奈伤痛 2020-12-12 09:02

Is there some difference between the following invocations?

path.join(__dirname, \'app\')

vs.

path.resolve(__dirname, \'app         


        
3条回答
  •  离开以前
    2020-12-12 09:50

    1) path.resolve creates the absolute path.

    The method creates absoulte path from right to left until an absolute path is constructed.

    For example:

    path.resolve('/a', 'b', 'c');     //    C:\a\b\c
    path.resolve('/a', '/b', 'c');    //    C:\b\c
    path.resolve('/a', '/b', '/c');   //    C:\c
    

    If absolute path is not generated, the method using current working directory:

    For example:

    path.resolve('a', 'b', 'c');     //    C:\{current_working_directory}\a\b\c
    

    2) path.join joins all path and the normalize the result

    For example:

    path.join('/a', '/b', '/c');   //   \a\b\c
    path.join('/a', '/b', 'c');    //   \a\b\c
    path.join('/a', 'b', 'c');     //   \a\b\c
    path.join('a', 'b', 'c');      //   \a\b\c
    

提交回复
热议问题