ENOENT error when using fs.writeFile - node.js

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'

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");
});

For me the problem was that the given filename contained unallowed characters on Windows.
Specifically, I tried adding a timestamp to the name e.g. 10:23:11 and the : were not allowed which caused this error.

Related

Error when trying to locate a json file, node (discord.js)

I'm getting an error when I try to find a file from my folder,
internal/modules/cjs/loader.js:969
throw err;
^
Error: Cannot find module 'badwords.json'
Here is the current code.
client.on("message", message => {
const badwords = require('badwords.json');
I can assure the file is in the same folder, people say it's a problem with my directory, I'm not too sure how to sort this out.
Thanks!

error when moving a file uploaded with multer: resource busy or locked

I have a node+express app that receives files and uploads them to a folder, for which I use multer.
In it's current form, my server.js has:
var multer = require('multer');
app.use(multer({ dest: './uploads/' }).single('files'));
which sends all files to the /uploads directory, with a randomly generated name to avoid conflicts.
That all works fine, however, I need to modify it so that it creates a folder with the randomly generated name and places the file inside (so if a file is randomly named "asdf", then it should end up as uploads/asdf/asdf.
Since the node way of moving files is using fs.rename, I included the following code in my upload route:
fs.rename('uploads/' + newFile.uploadname, 'uploads/' + newFile.uploadname + '/' + newFile.uploadname, function(err) {
if (err) throw err
console.log('Successfully renamed - AKA moved!')
})
(newFile.uploadname should be, and actually is, the name generated by multer).
When that code executes, I get this output:
D:\NodeApp\app\routes\private\upload.js:35
if (err) throw err
^
Error: EBUSY: resource busy or locked, rename 'D:\NodeApp\uploads\b9998bcbb10326c05f305a6a5a0adb9a' -> 'D:\NodeApp\up
loads\b9998bcbb10326c05f305a6a5a0adb9a\b9998bcbb10326c05f305a6a5a0adb9a'
at Error (native)
The file is uploaded properly but not moved, nor the directory created.
What is happening?
The problem was that I was trying to move a file named 'asdf' to a folder called also 'asdf'... Obviously (in restrospect) that folder can't be created because the name is already being used by the file that will be copied. Moving the file to a temporal folder first and then creating the folder solved the issue.

Gulp.src(): where to read files?

I have the following code, but it can't read files from src folder. Does anyone know the path where gulp read file from?
gulp.task('default', function() {
return gulp.src(__dirname + './src/*')
.pipe() ...//other code
});
Folder structure:
--app
--gulp
--gulpfile.js
--src
--(files-to-be-read, for example: 1.png)
The __dirname global returns the current directory and will not include aditional slash at the end, so concatenating './otherDir' will become '/dir./otherDir'.
Just remove the dot from the string to concatenate.
https://nodejs.org/docs/latest/api/globals.html
I would think - because I am not a Node.js expert - that Gulp uses the current working directory.
The directory which contains the package.json and the Gulp script.
So, when invoking Gulp in the app/gulp directory, you can get away with this:
gulp.task('default', function() {
return gulp.src('src/*')
.pipe() ...//other code
});
You can also use relative paths from that dir, like this:
gulp.task('other', function() {
return gulp.src('../../whatever/*')
.pipe() ...//other code
});

Err. ENOENT when renaming a file in node.js

I'm trying to upload a file in my node/express app, but everytime I've got the ENOENT error when renaming the file. My code is that:
var tmp_path = req.files.file.path;
fs.rename(tmp_path, target_path, function (err) {
if(err) throw err;
...
});
where target_path will be the destination path. If I do:
console.log('exists ' + fs.existsSync(tmp_path));
then my server logs:
exists true
Also, listing the contents of tmp directory shows that the file is there. What's the problem?
FS methods like fs.rename which create, move or rename files expect that any directories in the path already exist. When they do not, you'll get an ENOENT. Since very often what you mean is "make this file -- and any directories in the path I specify for it" you may want to consider using an NPM library that abstracts access to fs with methods that take care of such things.
There are quite a few options. For example fs-extra is one of the better-tested libraries. Using fs-extra you can use ensureDir in that operation to make the directory structure first if it does not yet exist.

Node.js: Check if file is an symbolic link when iterating over directory with 'fs'

Supervisor is a package for Node.js that monitors files in your app directory for modifications and reloads the app when a modification occurs.
This script interprets symbolic links as regular files and logs out a warning. I would like to fork Supervisor so that either this can be fixed entirely or that a more descriptive warning is produced.
How can I use the File System module of Node.js to determine if a given file is really an symbolic link?
You can use fs.lstat and then call statis.isSymbolicLink() on the fs.Stats object that's passed into your lstat callback.
fs.lstat('myfilename', function(err, stats) {
console.log(stats.isSymbolicLink());
});
Seems like you can use isSymbolicLink()
const files = fs.readdirSync(dir, {encoding: 'utf8', withFileTypes: true});
files.forEach((file) => {
if (file.isSymbolicLink()) {
console.log('found symlink!');
}
}

Resources