Meteor: Error: ENOENT when trying to access an image using node-gd
I cannot figure out why I am getting this "Error: ENOENT" error. Here is my Meteor server method:
createImage: function(coords) {
console.log('createImage')
console.log(coords.area)
console.log(coords.x)
console.log(coords.y)
console.log(coords.x2)
console.log(coords.y2)
console.log(coords.w)
console.log(coords.h)
var gd = Meteor.npmRequire('node-gd');
var path = Meteor.npmRequire('path');
var fs = Meteor.npmRequire('fs');
var source = 'forrest.png';
var target = 'compimages';
if (path.exists(target)) fs.unlink(target);
gd.openPng(source, function(png, path) {
if(png) {
console.log(png)
console.log(path)
}
}
);
}
Here is the output I get from it on the terminal:
=> Meteor server restarted
I20140827-15:30:18.451(-7)? createImage
I20140827-15:30:18.455(-7)? 27888
I20140827-15:30:18.456(-7)? 242
I20140827-15:30:18.459(-7)? 164
I20140827-15:30:18.459(-7)? 410
I20140827-15:30:18.459(-7)? 330
I20140827-15:30:18.459(-7)? 168
I20140827-15:30:18.460(-7)? 166
W20140827-15:30:18.527(-7)? (STDERR) path.exists is now called `fs.exists`.
I20140827-15:30:18.547(-7)? { [Error: ENOENT, open 'forrest.png'] errno: 34, code: 'ENOENT', path: 'forrest.png' }
I20140827-15:30:18.548(-7)? undefined
These are directories in ~ / myapp / server /
me@ubuntu:~/myapp/server$ ls
compimages forrest.png privateimages server.js user-setup.js
I want to access forrest.png and use node-gd to strip out a portion of it based on the agreed codes.
As I understand it, this error means the directory is missing. The png file that I am trying to access is in the same directory as its .js file (they are both in ~ / myapp / server /, so it doesn't seem like the problem to me. The only thing I can see is that the path is undefined. I have this set:
me@ubuntu:~/myapp/packages/npm/npm/node_modules$ ls
node-gd path
Can anyone see what's going on with this? I am using Meteor 0.9.0
source to share
If you are console.log(process.cwd())
in your server code, you will notice that the current working directory of your Meteor application project/.meteor/local/build/programs/server
, however, your code assumes that CWD project/server
.
What you can do is prefix your paths with the root folder of the project server, which is obtained by navigating 5 levels in the file system hierarchy.
var projectRootServer="../../../../../server";
var source=projectRootServer+"forrest.png";
It's not very elegant, but it works.
Unrelated but seems to be path.exists
outdated in favor fs.exists
, you should fix this as well.
source to share