I tried to uplaod file and move to new directory already exists.
follow Writing files in Node.js but I got the error:
Error: EISDIR, open '/Users/name/Sites/project/app/assets/images/UploadTemporary/'
at Error (native)
and I found Using Node.js I get, "Error: EISDIR, read" and Node.js Error: EISDIR, open Error similar error message, my UploadTemporary folder already exists do I mess something wrong?
I don't get it, if its not a directory what else can be?
var multipart = require('connect-multiparty');
var fs = require('fs');
var path = require('path');
var appDir = path.dirname(require.main.filename);
...
var sourceFile = req.files.file[0].path;
var destinationFile = appDir + '/assets/images/UploadTemporary/';
var source = fs.createReadStream(sourceFile);
var destination = fs.createWriteStream(destinationFile);
source.pipe(destination);
source.on('end', function () {
fs.unlinkSync(sourceFile);
});
When you are writing a file to a specific directory, you need to give the actual destination file name as well. Unlike cp command, the destination filename will not be inferred by fs module.
In your case, you are trying to write to a directory, instead of a file. That is why you are getting EISDIR error. To fix this, as you mentioned in the comments,
var destinationFile = appDir + '/assets/images/UploadTemporary/' + newfilename;
include the file name as well.
Related
error image: (https://i.stack.imgur.com/IesaI.jpg)
I tried to run the code but it resulted in an error that I could not open the json file saying that it did not exist
Because it can't find the file you want. You should give the absolute path to file.
const fs = require('fs');
const path = require('path');
fs.readFile(path.join(__dirname, 'database/registros.json'), {encoding: 'utf-8'}, function(err,data) {});
I am trying to unzip a gzipped file in Node but I am running into the following error.
Error: incorrect header check
at Zlib._handle.onerror (zlib.js:370:17)
Here is the code the causes the issue.
'use strict'
const fs = require('fs');
const request = require('request');
const zlib = require('zlib');
const path = require('path');
var req = request('https://wiki.mozilla.org/images/f/ff/Example.json.gz').pipe(fs.createWriteStream('example.json.gz'));
req.on('finish', function() {
var readstream = fs.createReadStream(path.join(__dirname, 'example.json.gz'));
var writestream = fs.createWriteStream('example.json');
var inflate = zlib.createInflate();
readstream.pipe(inflate).pipe(writestream);
});
//Note using file system because files will eventually be much larger
Am I missing something obvious? If not, how can I determine what is throwing the error?
The file is gzipped, so you need to use zlib.Gunzip instead of zlib.Inflate.
Also, streams are very efficient in terms of memory usage, so if you want to perform the retrieval without storing the .gz file locally first, you can use something like this:
request('https://wiki.mozilla.org/images/f/ff/Example.json.gz')
.pipe(zlib.createGunzip())
.pipe(fs.createWriteStream('example.json'));
Otherwise, you can modify your existing code:
var gunzip = zlib.createGunzip();
readstream.pipe(gunzip).pipe(writestream);
I am new to Browserify and trying the following:
I created a node server and trying to get a package called 'openbci' running on the browser.
so I have the following file structure:
Myapp
-...
-public
--app.js
--index.html
--openBCI.js
--...
--javascript
---openBCI
----bundle.js
---...
-node_modules
--openbci
---openBCIBoard.js
--browserify
--...
my app.js file sets the server to serve the public folder
// app.js
var express = require('express');
var app = express();
app.use(express.static('public'));
app.listen(myPort);
then I created the following openBCI.js
// openBCI.js
var OpenBCIBoard = require('openbci').OpenBCIBoard;
exports.OpenBCIBoard = OpenBCIBoard;
and finally launched the browserify command:
$ browserify public/openBCI.js > public/javascript/openBCI/bundle.js
but once called in my index.html file, I got an Uncaught TypeError: exists is not a function at Function.getRoot:
exports.getRoot = function getRoot (file) {
var dir = dirname(file)
, prev
while (true) {
if (dir === '.') {
// Avoids an infinite loop in rare cases, like the REPL
dir = process.cwd()
}
**if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) {**
// Found the 'package.json' file or 'node_modules' dir; we're done
return dir
}
if (prev === dir) {
// Got to the top
throw new Error('Could not find module root given file: "' + file
+ '". Do you have a `package.json` file? ')
}
// Try the parent dir next
prev = dir
dir = join(dir, '..')
}
}
It appears that it could not find the original path for the module.
Could you please tell me what is to change? Or if I understood at all how browserify works ? :)
I notice a few things that seem strange about the code.
exists is undefined in JavaScript or node. It appears to be an alias of fs.exists - is that right?
If so, fs.exists is deprecated. Per the documentation, you can achieve the same effect with fs.stat or fs.access. Note however that you should either supply a callback (preferable) or use the Sync version of these methods.
If you are trying to use file system tools in the browser you are going to run into problems because you are attempting to access the server's file system from the browser. There is a plugin, browserify-fs, that gives you an equivalent to fs in the browser. However, this seems to access the browser's local IndexedDB, not the storage on your server.
I would suggest running code that relies on server-side files on the server, rather than in the browser.
I have a function that should open a directory after it was created,
setTimeout(function()
{
var fs = require('fs');
console.log(newPath);
var open = fs.opensync(newPath, 'r');
}, 2500);
But this doesn't seem to work. I am getting the following errors
first is,
TypeError: undefined is not a function
at eval (eval at <anonymous> (file:///Users/proslav/Library/Developer/Xcode/DerivedData/trackingCore-ecxfviftqracjxhimcuhhhvyddso/Build/Products/Debug/trackingCore.app/Contents/Resources/timeBroFront.app/Contents/Resources/app.nw/js/jquery-1.10.2.min.js:3:4994), :43:18)
and second is,
Uncaught ReferenceError: require is not defined
I was thinking that it could be that my variable newpath is undefinded but the log shows me the right link.
The creation of the directory with var fs = require('fs'); works fine.
What am I doing wrong here?
I found out how it has to be done. Node-webkit offers a function for that. It is working on MAC and should also work on windows.
The function below is an example function. nw.gui and gui.Shell.showItemInFolder did the thing for me. Thx for the input.
/*---------
Open Folder
---------*/
function openFolder(path){
var gui = require('nw.gui');
gui.Shell.showItemInFolder(path);
}
In nw.js version 0.13 or later, use:
nw.Shell.showItemInFolder(fullpath);
Version < 0.13:
var gui = require('nw.gui');
gui.Shell.showItemInFolder(fullpath);
Note that the full path name is required. If it doesn't exist, it will fail silently.
If the path is something like c:\foo\bar.txt, it will open the folder foo and highlight the file bar.txt.
If the path is c:\foo\foo2, it will open the folder foo and highlight the folder foo2 (I expected it to open the folder foo2, but it will open the parent).
To find the fullpath of the running app, as we can't use node functions in our front-end (that's why you had the error trying to load the fs module), I've created a node module (utils.js) with the following:
exports.getFullPath = function(fileName) {
var path = require('path');
return path.resolve(__dirname, fileName);
}
In the front-end:
function openFolder(path) {
var utils = require('./utils');
var fullpath = utils.getFullPath(path);
nw.Shell.showItemInFolder(fullpath);
}
I am trying to reference an html document in a "self containing" module. The module is comprised of two files:
app_root/node_module/my_module/main.js
app_root/node_module/my_module/header.html
main.js contains:
module.exports.test = function() {
var doc = fs.readFileSync('./header.html');
console.log(doc);
}
when i run my program in app_root/program.js
require('my_module').test();
When i start my app, the current working directory is set to app_root. When it tries to read ./header.html it breaks because the paths aren't correct.
How would I find directory of the installed module without knowing anything about what is running it?
You can use __dirname to refer to the path of the current script.
So main.js would become:
module.exports.test = function() {
var doc = fs.readFileSync(__dirname + '/header.html');
console.log(doc);
}