最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - meteor js how to write a file to disk from the server - Stack Overflow

programmeradmin4浏览0评论

I am writing a meteor package 'myPackage' which needs to write a file onto disk using Npm FileSystem and Pah modules. The file should end up in the example-app/packages/myPackage/auto_generated/myFile.js, where example-app project has myPackage added.

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 /Uses/martinfox/tmp/auto-generated folder does exist

  1. Any ideas what is going wrong?
  2. Is it possible to obtain the absolute path to the meteor projects directory?

I am writing a meteor package 'myPackage' which needs to write a file onto disk using Npm FileSystem and Pah modules. The file should end up in the example-app/packages/myPackage/auto_generated/myFile.js, where example-app project has myPackage added.

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 /Uses/martinfox/tmp/auto-generated folder does exist

  1. Any ideas what is going wrong?
  2. Is it possible to obtain the absolute path to the meteor projects directory?
Share Improve this question edited Dec 2, 2013 at 10:13 mfox asked Dec 1, 2013 at 14:49 mfoxmfox 771 silver badge5 bronze badges 4
  • 1 Are you sure your directory begins with /Uses and not /Users? – C Blanchard Commented Dec 1, 2013 at 14:59
  • Also, regarding your second question, __dirname will give you the name of the directory that the currently executing script resides in – C Blanchard Commented Dec 1, 2013 at 14:59
  • @CBlanchard Sorry my bad typo Question 1 now works but I don't seem to be able to get var filePath = path.join( __dirname + 'test, myFile.txt) – mfox Commented Dec 2, 2013 at 10:16
  • I get ref error __dirname undefined. Also tried fs.__dirname – mfox Commented Dec 2, 2013 at 10:27
Add a ment  | 

2 Answers 2

Reset to default 11

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

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 it will help you

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

This would avoid the extra stuff .meteor/local/build/programs/server

发布评论

评论列表(0)

  1. 暂无评论