简体   繁体   English

如何使用path.resolve进行导航?

[英]How to navigate using path.resolve?

I am inside of a folder src and want to create files insides another folder called dist . 我在src文件夹中,想在另一个名为dist文件夹中创建文件。 Below is what I want to do from inside the src folder. 下面是我想从src文件夹中执行的操作。 Create a json file inside of dist/JSON/{file goes here} dist/JSON/{file goes here}创建一个json文件

main
- src
  index.js
- dist
  - JSON
    file.json

So my code is: 所以我的代码是:

const file = path.resolve(__dirname, `/dist/JSON/${filename}.json`);
await fs.outputJson(file, {name: "name"})

This is putting the file however inside of src/dist/JSON/${filename}.json and I want dist/JSON/${filename}.json 但这会将文件放在src/dist/JSON/${filename}.json而我想要dist/JSON/${filename}.json

Since you want to go to the parent directory before navigating, you should add ../ to your path.resolve() and it should fix your issue. 由于要在导航之前转到父目录,因此应将../添加到path.resolve() ,这样可以解决您的问题。

const file = path.resolve(__dirname, `../dist/JSON/${filename}.json`);
await fs.outputJson(file, {name: "name"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM