I am trying to concatenate a bunch of files inside a folder using node's fs.
The problem is that I get a
Uncaught ReferenceError: fs is not defined
When attempting to use fs.readFileSync with the files array in the code below.
var fs = require('fs');
var path = require('path');
var output = "";
var files = fs.readdirSync('./content');
console.log(files); //fs works here
for(var i = 0; i < files.length; i ++) {
output += fs.readFileSync(path.join('./content', files[i]), 'utf8') + '\n';
}
module.exports = output;
At first I thought it was some kind of scope issue, but then I came across this issues fs is not defined error when readFileSync is passed a path variable which basically says:
You can't use variables since the expression has to be statically analyzable, i.e. Known at build time, not run time.
So is there any other way to go about concatenating those files?
Thanks for any help!
Related
So I have a group of files organized like this:
./main/folder1/files.js
./main/differentfolder/files.js
./main/test/files.js
./main/test/files.js
The files are NOT named files.js, it's my way of saying that there's a lot of files in that folder.
How do I access all the files shown above without doing something like this:
const commandFiles = fs.readdirSync(`./main/folder1/files.js`)
const commandFiles2 = fs.readdirSync(`./main/differentfolder/files.js`)
const commandFiles3 = fs.readdirSync(`./main/test/files.js`)
//and so on
Just grab all the Javascript files inside "main" regardless of the folder name, both using "fs" and "require". I'd expect it to be the same. There is nothing but folders in ./main.
EDIT: I just want it so fs can check /main//files instead of what's inside of main
I easily solved this problem by using the require-all package which was something installed with the other packages I'm using for my app:
const fs = require('fs');
var folders = fs.readdirSync('./main/').filter(n => n !== '.DS_Store') //filter out that disgusting macOS file
folders.forEach(async (folder) => {
var cmdfolder = Object.values(require('require-all')(__dirname + `/main/${folder}`))
cmdfolder.forEach(async (key) => {
var file = require(`./main/${folder}/${key.fileName}`)'
//register file
})
})
I am trying to minify html file using the node module html-minifier. In order to this I have created this little node.js file which should be able to do this
'use strict'
var fs = require('fs');
var minifier = require('html-minifier').minify;
var htmlFile = fs.readFileSync("users/email/test.html");
var output = minifier(htmlFile, {
removeAttributeQuotes: true
});
process.stdout.write(output);
but when I run the program I get the following error.
TypeError: value.replace is not a function
Any idea why this is happening. I am using version 4.0.0 of html-minifier
Since you haven't specified a text encoding, readFileSync returned a Buffer, not a string. See the readFileSync documentation.
If you know the encoding to use, you can specify it as a second argument:
var htmlFile = fs.readFileSync("users/email/test.html", "utf8");
Processing a bunch of files using Node and I need to separate the file name from the directory. Does node have a simple way to do this without additional dependencies? I could use an NPM package like Filename Regex, but thought I'd check if there something available out of the box?
So for example suppose we have src/main/css/file.css. Hoping something like this is possible:
const fs = require('fs');
const path = fs.filePath(String pathAndFileName); //path = src/main/css
const file = fs.fileName(String pathAndFileName); //file = file.css
The utilities for manipulating file paths are in the path module.
https://nodejs.org/api/path.html
const {dirname, basename} = require('path');
const path = dirname(String pathAndFileName);
const file = basename(String pathAndFileName);
I am trying to delete all pictures of an directory. But getting error on directory path. And also dont know how to get all pictures path & delete all of them.
My directory structure :
server
-> app.js
tmp
-upload
-- pic.jpg
-- pic2.jpg
-- pic3.jpg
I have tried this :
var dir = require('../tmp/upload');
var fs = require('fs');
var promise = require('bluebird');
fs.readdir(dir).then(function(file) {
console.log(data)
}).catch(function(err){
console.log
})
But getting error : Cannot find module '../tmp/upload'
Need help to get the path & all pictures on upload folder & delete them.
Thanks in advance
You got this error simply because you actually required a module from the relative path instead of resolving it. In order to resolve a relative path to an absolute path, you need to use path.resolve, not require.
var path = require('path');
var dir = path.resolve('../tmp/upload');
const fsPromises = require('fs').promises
// For ES syntax: import { promises as fsPromises } from 'fs'
const directory = 'your/directory/path/here'
await fsPromises.rmdir(directory, {
recursive: true
})
I have a problem, i am trying to create an object that i can use over and over in a separate javascript file. I have a file called foo.js and another file called boo.js. I want to create the object in boo.js In my server.js file i required foo.js, and it works fine i can access foo.js. I want to be able to access boo.js from foo.js. I keep getting errors when i require boo.js in foo.js and i cant access it. Is There a way to do this?
here is my code
//boo.js
var obj = function () {
return {
data: 'data'
}
}
module.exports = {
obj: obj
}
foo.js
//foo.js
var request = require('request');
var x = require('./modules/boo')
var random= function() {
return x.obj();
}
module.exports = {
random: random
}
If they are in the same directory you will want to require like so var x = require('./boo'). The ./ is relative to the current directory.
they are both in the same directory
In that case, you'll want to remove the modules/ from the path:
var x = require('./boo');
require() is aware of the current script's location and bases relative paths from the script's own parent directory.
The ./ at the start of the path will refer to the same directory as __dirname, which seems to be modules/.
console.log(__dirname);
// "/project-path/modules"
Including modules/ in the path will result in doubling it:
var path = require('path');
console.log(path.resolve(__dirname, './modules/boo'));
// "/project-path/modules/modules/boo"
(Side note: The fs module does not behave the same way. Relative paths for it are based from the current working directory or process.cwd().)