What is the current directory used by fs module functions?

It’s the directory where the node interpreter was started from (aka the current working directory), not the directory of script’s folder location (thank you robertklep). Also, current working directory can be obtained by: process.cwd() For more information, you can check out: https://nodejs.org/api/fs.html EDIT: Because you started app.js by node app.js at ExpressApp1 directory, every relative … Read more

Module not found: Can’t resolve ‘fs’ in Next.js application

If you use fs, be sure it’s only within getInitialProps or getServerSideProps. (anything includes server-side rendering). You may also need to create a next.config.js file with the following content to get the client bundle to build: For webpack4 module.exports = { webpack: (config, { isServer }) => { // Fixes npm packages that depend on … Read more

Get all files recursively in directories NodejS

It looks like the glob npm package would help you. Here is an example of how to use it: File hierarchy: test ├── one.html └── test-nested └── two.html JS code: const glob = require(“glob”); var getDirectories = function (src, callback) { glob(src + ‘/**/*’, callback); }; getDirectories(‘test’, function (err, res) { if (err) { console.log(‘Error’, … Read more

What are the pros and cons of fs.createReadStream vs fs.readFile in node.js?

A better approach, if you are just going to hook up “data” to “write()” and “close” to “end()”: // 0.3.x style fs.createReadStream(filename, { ‘bufferSize’: 4 * 1024 }).pipe(response) // 0.2.x style sys.pump(fs.createReadStream(filename, { ‘bufferSize’: 4 * 1024 }), response) The read.pipe(write) or sys.pump(read, write) approach has the benefit of also adding flow control. So, if … Read more

writeFile no such file or directory

This is probably because you are trying to write to root of file system instead of your app directory ‘/avatar/myFile.png’ -> __dirname + ‘/avatar/myFile.png’ should do the trick, also check if folder exists. node.js won’t create parent folder for you.

NodeJS accessing file with relative path [duplicate]

You can use the path module to join the path of the directory in which helper1.js lives to the relative path of foobar.json. This will give you the absolute path to foobar.json. var fs = require(‘fs’); var path = require(‘path’); var jsonPath = path.join(__dirname, ‘..’, ‘config’, ‘dev’, ‘foobar.json’); var jsonString = fs.readFileSync(jsonPath, ‘utf8’); This should … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)