How to access /tmp folder in Lambda with in Node? - node.js

I'm trying to save a processed image momentally in /tmp folder but it's not working for me. Assuming it's under the root folder I'm trying to get it this way:
let tempraryImageDirectory: string;
if (process.env.DEV && process.env.DEV === 'Yes') {
tempraryImageDirectory = path.join(__dirname, `../../tmp/`);
} else {
tempraryImageDirectory = path.join(__dirname, `./tmp/`);
}
The else choice here is to test locally. I don't want to create a /tmp folder in the root directory. Locally everything is very fine. But in Lambda at the moment any operation happens on the directory CloudWatch never shows any of my logs written after that and my function fails for unknown reason.
Any idea if I'm addressing the /tmp folder correctly?

The directory is just /tmp, it is not relative to the working directory:
let tempraryImageDirectory: string;
if (process.env.DEV && process.env.DEV === 'Yes') {
tempraryImageDirectory = path.join(__dirname, `../../tmp/`);
} else {
tempraryImageDirectory = '/tmp/';
}
You may also wish to rename your variable to include the o in temporary if you did not leave it out on purpose.

Change this: ./tmp/, i.e. "the /tmp directory under the current working directory"
To this: /tmp/, i.e. "the /tmp directory under the root file system"

Related

How to delete all files and subdirectories in a directory with Node.js

I am working with node.js and need to empty a folder. I read a lot of deleting files or folders. But I didn't find answers, how to delete all files AND folders in my folder Test, without deleting my folder Test` itself.
I try to find a solution with fs or extra-fs. Happy for some help!
EDIT 1: Hey #Harald, you should use the del library that #ziishaned posted above. Because it's much more clean and scalable. And use my answer to learn how it works under the hood :)
EDIT: 2 (Dec 26 2021): I didn't know that there is a fs method named fs.rm that you can use to accomplish the task with just one line of code.
fs.rm(path_to_delete, { recursive: true }, callback)
// or use the synchronous version
fs.rmSync(path_to_delete, { recursive: true })
The above code is analogous to the linux shell command: rm -r path_to_delete.
We use fs.unlink and fs.rmdir to remove files and empty directories respectively. To check if a path represents a directory we can use fs.stat().
So we've to list all the contents in your test directory and remove them one by one.
By the way, I'll be using the synchronous version of fs methods mentioned above (e.g., fs.readdirSync instead of fs.readdir) to make my code simple. But if you're writing a production application then you should use asynchronous version of all the fs methods. I leave it up to you to read the docs here Node.js v14.18.1 File System documentation.
const fs = require("fs");
const path = require("path");
const DIR_TO_CLEAR = "./trash";
emptyDir(DIR_TO_CLEAR);
function emptyDir(dirPath) {
const dirContents = fs.readdirSync(dirPath); // List dir content
for (const fileOrDirPath of dirContents) {
try {
// Get Full path
const fullPath = path.join(dirPath, fileOrDirPath);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
// It's a sub directory
if (fs.readdirSync(fullPath).length) emptyDir(fullPath);
// If the dir is not empty then remove it's contents too(recursively)
fs.rmdirSync(fullPath);
} else fs.unlinkSync(fullPath); // It's a file
} catch (ex) {
console.error(ex.message);
}
}
}
Feel free to ask me if you don't understand anything in the code above :)
You can use del package to delete files and folder within a directory recursively without deleting the parent directory:
Install the required dependency:
npm install del
Use below code to delete subdirectories or files within Test directory without deleting Test directory itself:
const del = require("del");
del.sync(['Test/**', '!Test']);

Read/Write files from relative paths after TypeScript compilation in node

I have the following folder structure:
/
/src/
file.ts
one.txt
/resources/
two.txt
and in file.ts I want to read contents of one.txt and two.txt by doing something like the following:
import fs from 'fs';
import path from 'path';
// sync is bad.
fs.readFileSync('one.txt');
fs.readFileSync(path.resolve(__dirname, '../resources/file.txt'));
Everything works just fine when using ts-node.
The problem is that when I run tsc it compiles all files to /dist (which I have told the compiler to do in tsconfig.json by setting outDir to ./dist), but both fs.readFileSync(...) fail, because the *.txt files are not copied to /dist, so fs can not find the files.
Now, my question is: Is there a beautiful way to handle this, and make fs read and writes work as expected both, when using ts-node, and after tsc?
I've managed to handle this in several projects by doing something weird like:
// file.ts
const getResourcesDir = () => {
const dir = path.basename(path.dirname(__dirname));
if (dir === 'dist') {
return path.resolve(__dirname, '../../resources');
} else {
return path.resolve(__dirname, '../resources');
}
}
But this seems just wrong. I believe that there should be a nicer solution, but I can't find it.
dir = path.basename(path.dirname(__dirname));
const certDir = (dir === 'dist' ? './dist/cert': '../cert');
console.log('Cert file', certDir, `${certDir}/server-key.pem`);
console.log('Key file', certDir, `${certDir}/server-key.pem`);

Node FS not finding folder

Using Node, I create a folder and then have a file in that folder. I created a function to delete it, but it absolutely refuses to find the folder.
Here's my function:
function deleteFile(path) {
if( !fs.existsSync(path) ) {
setTimeout(deleteFile(path), 500)
} else {
fs.readdirSync(path).forEach(function(file){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
It will continue to recurse until it hits maximum call stack and crash, but the folder exists LONG before that happens. As you can see, there exists both the folder and the file inside of it. Could someone please help me fix this?
If anyone else comes across this issue, I figured it out. When the folder is created, it gives the incorrect permissions. I used fs.chmod to change permissions beforehand, and that fixed it.

Node fs showing that file is not directory when it is

I have the following code...
console.log("looking for "+ path+" "+fs.lstatSync(path).isDirectory());
with path == "/Volumes/Macintosh HD" I get
looking for /Volumes/Macintosh HD false
I also tried converting to "/Volumes/Macintosh\ HD" but then it can't even find the file.
Why is this not showing up as a directory?
This is because /Volumes/Macintosh HD is actually a symlink to /. It's not actually a directory.
You can do this instead:
console.log("looking for "+ path+" symlink: "+fs.lstatSync(path).isSymbolicLink());
// outputs: looking for /Volumes/Macintosh HD symlink: true
If you wanted to integrate this in your logic to check to see if the symlink is pointing to a directory, you can try something like this:
var path = '/Volumes/Macintosh HD';
if (fs.lstatSync(path).isSymbolicLink()) {
// replace 'path' with the real path if it's a symlink
path = fs.readlinkSync(path);
}
if (fs.lstatSync(path).isDirectory()) {
console.log('it is a directory!');
}

Node.js fs.unlink function causes EPERM error

I'm using fs.unlink() to delete a file and I receive the following error:
uncaught undefined:
Error: EPERM, Operation not permitted '/Path/To/File'
Anyone know a why this is happening?
You cannot delete a directory that is not empty.
And fs.unlinkSync() is used to delete a file not a folder.
To remove an empty folder, use
fs.rmdir()
to delete a non empty folder, use this snippet:
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file) {
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
Snippet from stackoverflow: Is node.js rmdir recursive ? Will it work on non empty directories?
If you want to achieve something like rm -rf does, there is a package from npm called rimraf which makes it very easy.
Maybe the Path of the file is located is erroneus.
if not, try with fs.unlinkSync()
Yes, you don't have permission to delete/unlink that file. Try again with more rights or verify that you're giving it the right path.

Resources