Meteor js how to write a file to disk from the server

I am writing a 'myPackage' meteor packet, which should write a file to disk using the Npm FileSystem and Pah modules. The file should end in the example-app / packages / myPackage / auto_generated / myFile.js, where the sample application is added by myPackage.

fs = Npm.require( 'fs' ) ; path = Npm.require( 'path' ) ; Meteor.methods( { autoGenerate : function( script ) { var myPath = '/Users/martinfox/tmp/auto-generated' ; var filePath = path.join(myPath, 'myFile.js' ) ; console.log( filePath ) ; // shows /Uses/martinfox/tmp/auto-generated/myFile.js var buffer = new Buffer( script ) ; fs.writeFileSync( filePath, buffer ) ; }, } ); 

When I run the code above (server side only) I get

 Exception while invoking method 'autoGenerate' Error: ENOENT, no such file or directory '/Uses/martinfox/tmp/auto-generated/myFile.js' 

Note / Usage / martinfox / tmp / automatically created folder exists

  • Any ideas what is going wrong?
  • Is it possible to get an absolute path to the catalog of meteor projects?
+4
source share
2 answers

To get the path to the project, you can do this: from main.js stored in the root of your application.

 var fs = Npm.require('fs'); __ROOT_APP_PATH__ = fs.realpathSync('.'); console.log(__ROOT_APP_PATH__); 

You can also check if your folder exists:

 if (!fs.existsSync(myPath)) { throw new Error(myPath + " does not exists"); } 

Hope this helps you.

+9
source

If you are just looking for an absolute path for your application, you can simply do var base = process.env.PWD , which gives: /Users/[username]/[app-name]

This will avoid unnecessary things .meteor/local/build/programs/server

+4
source

Source: https://habr.com/ru/post/1238271/


All Articles