Tazaf
Tazaf

Reputation: 390

When I try to access process.env.PWD in my Meteor app, it says that it's undefined

I just started to experiment with Node.js and Meteor for a project of mine. For this project, I need to upload files. Therefore, I've installed this package : meteor-uploads. During the Quick Start part, I'm asked to put this code in my server/init.js file :

//file:/server/init.js
Meteor.startup(function () {
  UploadServer.init({
    tmpDir: process.env.PWD + '/.uploads/tmp',
    uploadDir: process.env.PWD + '/.uploads/',
    checkCreateDirectories: true //create the directories for you
  })
});

Problem : my process.env.PWD is undefined and that obviously crashes the app whenever I try to upload file(s). With the node.js prompt, I've tried to access the process variable (it worked), the process.env variable (it worked again) and finally the process.env.PWD variable (undefined).

I've hardcoded the path to the different directory required by the package as a work-out and that worked, the file was uploaded (but it's not very smoothed as I will need to change that for others environnement).

But I can't manage to understand why I don't have a process.env.PWD. If anyone has any idea or question, be my guest (and thanks in advance).

EDIT : Oh, and I tried to find an answer on the Internets without any success, hence my lack of understanding.

Upvotes: 3

Views: 5365

Answers (3)

kshitij
kshitij

Reputation: 624

You can use:

 var homeDir = process.env.HOMEPATH;
 tmpDir: homeDir + '/uploads/tmp'`

Upvotes: 2

D Durham
D Durham

Reputation: 1341

I know this is a bit late... but I've been chasing this for awhile and it's a bug in the uploads package and PWD no longer works in Meteor. On Windows, you can pre create the directories (I am using meteor_bootstrap.serverDir to get the base and then backing up from there to the app root). But you CANNOT call checkCreateDirectories because of https://github.com/tomitrescak/meteor-uploads/issues/66 ( see my comment under user genyded which has the details).

So, until that issue is addressed, if you want something in the app root...

var fs = Npm.require('fs');
var path = Npm.require('path');
var meteor_root = fs.realpathSync(__meteor_bootstrap__.serverDir + '/../' );
var application_root = fs.realpathSync( meteor_root + '/../' );

// if running on dev mode
if( path.basename( fs.realpathSync(meteor_root + '/../../../' ) ) == '.meteor'){
    application_root =  fs.realpathSync( meteor_root + '/../../../../' );
}

Meteor.startup(function () {
  UploadServer.init({
    tmpDir: application_root + '/../../../../../.uploads/tmp',
    uploadDir: application_root + '/../../../../../.uploads',
  })
});

...but you MUST pre create the dirs first.

Upvotes: 1

Pierre Inglebert
Pierre Inglebert

Reputation: 3930

Use process.cwd() if you want the current working directory instead of relying on possibly missing environment variables.

Upvotes: 2

Related Questions