最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node fs.writefile with absolute path - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

according 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
  }
})();
发布评论

评论列表(0)

  1. 暂无评论