ENOENT error when using fs.writeFile

Tom

Trying to write to a file using fs.writeFile into a sibling directory. This works fine when using Sitemap.xml into the same directory, but not with the relative path. The public directory exists and it gives the same error whether or not Sitemap.xml exists.

Relevant dir structure:

/public
   Sitemap.xml
   app files
/create-sitemap
    index.js - file containing code below
app.js

fs.write('../public/Sitemap.xml', data.toString(), function(err) {
    if (err) throw err;
    console.log("Wrote sitemap to XML");
});


Toms-MacBook-Pro:moviehunter tomchambers$ node create-sitemap/index.js

/Users/tomchambers/projects/project/create-sitemap/index.js:88
        if (err) throw err;
                       ^
Error: ENOENT, open '../public/Sitemap.xml'
Rodrigo Medeiros

When you use relative paths in node, they're related to the node process. So, if you run your script like node create-sitemap/index.js from the /Users/tomchambers/projects/project/ directory, it'll look for the /Users/tomchambers/projects/public/Sitemap.xml file, which doesn't exist.

In your case, you could use the __dirname global variable, that returns, as the docs say:

The name of the directory that the currently executing script resides in.

So your code should looks like this:

var path = require('path');

fs.write(path.join(__dirname, '../public/Sitemap.xml'), data.toString(), function(err) {
  if (err) throw err;
  console.log("Wrote sitemap to XML");
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error with NodeJS FS (writeFile)

From Dev

Using fs.stat and fs.writeFile

From Dev

ENOENT using fs.appendFile()

From Dev

Using fs.readdir and fs.statSync returns ENOENT, no such file or directory error

From Dev

Execute code after fs.writeFile using async/await

From Dev

Using fs.readdir returns ENOENT on vps, but not on localhost

From Dev

Meteor: Error: ENOENT when trying to access an image using node-gd

From Dev

fs writefile new line not working

From Dev

fs writefile new line not working

From Dev

Running `fs.writeFile` with JSPM

From Dev

ENOENT Error when deployed to meteor server

From Dev

ENOENT error when doing ionic start

From Dev

ENOENT Error when deployed to meteor server

From Dev

Looks like when I do fs.writeFile(), the changed file restarts nodemon. How to make it not restart?

From Dev

How to ensure all directories exist before to fs.writeFile using node.js

From Dev

node - fs.writeFile creates a blank file

From Dev

fs.writeFile() doesn't return callback

From Dev

nodejs fs.writeFile failing after post

From Dev

nodejs fs.writeFile failing after post

From Dev

fs.writeFileSnyc/fs.writeFile writes a corrupted file

From Dev

Error: ENOENT when renaming file in node/express app

From Dev

"Error: spawn mongoexport ENOENT" when running dockerized node app

From Dev

404 Error: ENOENT, stat '/public/ when starting app from grunt

From Dev

ENOENT error when trying to install Yeoman from npm

From Dev

npm throws ENOENT error when trying to install module

From Dev

Node.js: Error: spawn ENOENT while using GM module

From Dev

How to suppress awk output when using FS?

From Dev

ENOENT, no such file or directory on fs.mkdirSync

From Dev

ENOENT: no such file or directory writing file with fs

Related Related

  1. 1

    Error with NodeJS FS (writeFile)

  2. 2

    Using fs.stat and fs.writeFile

  3. 3

    ENOENT using fs.appendFile()

  4. 4

    Using fs.readdir and fs.statSync returns ENOENT, no such file or directory error

  5. 5

    Execute code after fs.writeFile using async/await

  6. 6

    Using fs.readdir returns ENOENT on vps, but not on localhost

  7. 7

    Meteor: Error: ENOENT when trying to access an image using node-gd

  8. 8

    fs writefile new line not working

  9. 9

    fs writefile new line not working

  10. 10

    Running `fs.writeFile` with JSPM

  11. 11

    ENOENT Error when deployed to meteor server

  12. 12

    ENOENT error when doing ionic start

  13. 13

    ENOENT Error when deployed to meteor server

  14. 14

    Looks like when I do fs.writeFile(), the changed file restarts nodemon. How to make it not restart?

  15. 15

    How to ensure all directories exist before to fs.writeFile using node.js

  16. 16

    node - fs.writeFile creates a blank file

  17. 17

    fs.writeFile() doesn't return callback

  18. 18

    nodejs fs.writeFile failing after post

  19. 19

    nodejs fs.writeFile failing after post

  20. 20

    fs.writeFileSnyc/fs.writeFile writes a corrupted file

  21. 21

    Error: ENOENT when renaming file in node/express app

  22. 22

    "Error: spawn mongoexport ENOENT" when running dockerized node app

  23. 23

    404 Error: ENOENT, stat '/public/ when starting app from grunt

  24. 24

    ENOENT error when trying to install Yeoman from npm

  25. 25

    npm throws ENOENT error when trying to install module

  26. 26

    Node.js: Error: spawn ENOENT while using GM module

  27. 27

    How to suppress awk output when using FS?

  28. 28

    ENOENT, no such file or directory on fs.mkdirSync

  29. 29

    ENOENT: no such file or directory writing file with fs

HotTag

Archive