admin管理员组文章数量:1320888
I have a node application that emits HTML files. This is the jest of how it works:
const fs = require('fs');
const outputPath = './dist/html/';
// code that generates names and content
const currentFile = `${outputPath}${name}.html`;
const content = '...';
fs.promises.writeFile(currentFile, content, 'utf8');
This works as intended, but generally it is a bad practice to write relative path this way (this works on Mac but probably would not work on Windows machine).
const fs = require('fs');
const path = require('path');
const outputPath = path.join(__dirname, 'dist', 'html');
// code that generates names and content
const currentFile = path.join(outputPath, `${name}.html`);
const content = '...';
fs.promises.writeFile(currentFile, content, 'utf8');
This works, but it creates an entire path (User/my.name/Documents/projects/my-project/dist/html/my-file.html) within my project, since fs.writeFile
writes the file relative to the working directory.
Can I make fs
write file to the absolute path? Alternatively, what is the proper way of generating relative paths?
I ended up using
const outputPath = `.${path.delimiter}dist${path.delimiter}ads${path.delimiter}`;
But this does not seem like the best possible solution.
I have a node application that emits HTML files. This is the jest of how it works:
const fs = require('fs');
const outputPath = './dist/html/';
// code that generates names and content
const currentFile = `${outputPath}${name}.html`;
const content = '...';
fs.promises.writeFile(currentFile, content, 'utf8');
This works as intended, but generally it is a bad practice to write relative path this way (this works on Mac but probably would not work on Windows machine).
const fs = require('fs');
const path = require('path');
const outputPath = path.join(__dirname, 'dist', 'html');
// code that generates names and content
const currentFile = path.join(outputPath, `${name}.html`);
const content = '...';
fs.promises.writeFile(currentFile, content, 'utf8');
This works, but it creates an entire path (User/my.name/Documents/projects/my-project/dist/html/my-file.html) within my project, since fs.writeFile
writes the file relative to the working directory.
Can I make fs
write file to the absolute path? Alternatively, what is the proper way of generating relative paths?
I ended up using
const outputPath = `.${path.delimiter}dist${path.delimiter}ads${path.delimiter}`;
But this does not seem like the best possible solution.
Share Improve this question asked Jun 5, 2020 at 13:10 David VodrážkaDavid Vodrážka 631 gold badge1 silver badge9 bronze badges 3-
1
You can simply just write the entire absolute path. If you begin a path with a slash, it should be absolute. For example, if I put
fs.promises.WriteFile("/stuff/index.html", "Hello, World!");
, then, on my Windows machine, it would put "C:/stuff/index.html" – Daniel Reynolds Commented Jun 5, 2020 at 13:34 -
1
I didn't know putting
/
in front of a path makes it absolute. Thank you, it works now as intended :) – David Vodrážka Commented Jun 5, 2020 at 13:48 - Great! I'm glad I could help. :3 – Daniel Reynolds Commented Jun 5, 2020 at 13:52
1 Answer
Reset to default 5according to the docs, 'fs' module works with both relative and absolute paths.
i guess you issue was somehow related to path building.
here is the working code:
const { promises: fsp } = require('fs');
const { join } = require('path');
const fileName = 'file.html';
const content = '...';
(async () => {
try {
await fsp.writeFile(join(process.cwd(), 'dist', 'html', fileName), content);
} catch (error) {
// handling
}
})();
本文标签: javascriptNode fswritefile with absolute pathStack Overflow
版权声明:本文标题:javascript - Node fs.writefile with absolute path - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742007345a2412227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论