0

In my nodeJS application (meteorJS) I need to use an absolut path for creating a file via fs.createWriteStream:

const absolutePath = '/Users/Anybody/Documents/Project/imports/temp'
if (Meteor.isServer) {
  const result = new Promise(function (resolve, reject) {
    const gfs = Grid(
      MongoInternals.defaultRemoteCollectionDriver().mongo.db,
      MongoInternals.NpmModule
    )
    const readerStream = gfs.createReadStream({ _id: id })
    const writerStream = fs.createWriteStream(absolutePath + 'temp.mp4')
    // ...
  })
}

This is how the code looks like for development, which I do on macOS. The production version of the application is deployed to an ubuntu server. So the absolute path is different.

I tried to use path:

const path = require('path')
const absolutePath = path.resolve('/imports/temp')

But this gives me only /imports/temp (on macOS)

user3142695
  • 15,844
  • 47
  • 176
  • 332
  • Possible duplicate of [Determine project root from a running node.js application](https://stackoverflow.com/questions/10265798/determine-project-root-from-a-running-node-js-application) –  Sep 12 '17 at 22:13

1 Answers1

0

You can use process.env.PWD to get your base path of you project. Then you can add your remaining folder segment path.

var base = process.env.PWD;
const absolutePath = base + '/imports/temp';
Pankaj Jatav
  • 2,158
  • 2
  • 14
  • 22