I have a problem with importing any file as a file instance from local folder.
Can you help me please?
I can't find anything in 'fs' or 'path', maby because I don't know what to see
I want to get File and pass it to my JS applicatin as a File instance.
Start by using a path relative to your script and the __dirname directive.
fs.readFileSync(__dirname + '/myfile.txt')
You may also wish to lose the / using path to make your code more portable.
const path = require('path')
fs.readFileSync(path.join(__dirname, 'myfile.txt')
In regards to format, the result will be Buffer. If you want a string,
fs.readFileSync(filename,'UTF-8')
If you want a blob, see stackoverflow.com/questions/14653349
Related
I've a code like this
const fileData = fs.createReadStream('Google Sheet Link');
The error I am getting is this.
This is probably due to fs trying to read from an absolute path, rather than directly from the URL.
The path, it is taking is "Directory to which file is located" + "Google sheet link"
I only want to open google sheet link in the createReaderStream Path.
I've hidden the google sheet path due to privacy reasons.
The createReadStream() method is an inbuilt application programming interface of fs module which allow you to open up a file/stream and read the data present in it.
fs.createReadStream( path, options )
Parameters: This method accept two parameters as mentioned above and described below:
path: This parameter holds the path of the file where to read the file. It can be string, buffer or URL.
options: It is an optional parameter that holds string or object.
Now, To avoid paths issues, it's recommended to use path.join, like:
const path = require('path');
const pathGoogleSheet = path.join(__dirname, 'Sheet name');
const readableStream = fs.createReadStream(pathGoogleSheet);
Here, you're creating a path to the google sheet referring to the current directory of the script stored in global variable called __dirname.
I am using Netlify CMS. I want to import all the slides for a carousel into my component. I made a collection called slider and added a few slides. That created two markdown files (one for each slide) in public/content/slider/. I would like to import them all into an iteratable object so I can build the carousel.
Because I have a webpack loader set up for markdown files, I can import a single markdown file no problem, like this:
import post from '../public/content/posts/[post name].md
But when I try to use require.context, require-context, or import fs, it's no good. So I decide to try requiring those libs from within getStaticProps. But __dirname in getStaticProps is /, the root of my computer's filesystem.
All the getStaticProps examples use data fetching. I'm missing some info. How can I import all the markdown files in the /slides/ folder?
This is a known issue in Next.js (see related discussion #32236), __dirname incorrectly resolves to / - you should use process.cwd() instead.
From the Next.js Reading files documentation:
Files can be read directly from the filesystem in getStaticProps.
In order to do so you have to get the full path to a file.
Since Next.js compiles your code into a separate directory you can't
use __dirname as the path it will return will be different from the
pages directory.
Instead you can use process.cwd() which gives you the directory where
Next.js is being executed.
I'm currently using "require(./file.json)" because I can't get anything else to work.
So I want to be able to reload config file so I can disable / enable some commands in my bot without having to restart it.
Any Help or Suggestion will be appreciated!
If JSON doesn't work then any other file that can be used as a "config/db" is fine!
Thank you!
To read a JSON file, you need to use the built-in fs module.
const fs = require('fs');
const myFile = JSON.parse(fs.readFileSync('file.json'));
//edit myFile however you want
fs.writeFileSync('file.json', JSON.stringify(myFile, null, 4));
use fs to read file contents, instead of include or require, When you make include, it will read once in run-time only.
Also you can use require inside functions, which makes read in every function call. This is not pretty (In my opinion), But it works.
Quick question from a NodeJs beginner:
I'm using express-formidable in order to upload files to my server. How do I save them? n_n'
Inspecting the variable that holds the file I have:
But I don't know what to do with it. I have checked some methods from fs and tried writeFile but of course it only writes [Object object].
Thanks
the File here not a browser's File, just a common object, but looks like File.
for avoid out of memory, the file has already saved in a temporary folder
you can just do like this:
const {name, path} = req.files.input
fs.writeFileSync(name, fs.readFileSync(path))
I have the following code in NodeJs,
var fs = require('fs');
var data = fs.readFileSync('client/assets/images/car-img.jpg');
This client/assets/images/car-img.jpg is the location of image in system i.e relative path. I want to get file contents from absolute path like this. Consider, getting Google image from this absolute path,
var data = fs.readFileSync('https://www.google.co.in/images/srpr/logo11w.png');
How should I read data using readFileSync? Any help appreciated.