我正在编写一个meteor包'myPackage',它需要使用Npm FileSystem和Pah模块将文件写入磁盘.该文件最终应该在example-app/packages/myPackage/auto_generated/myFile.js中,其中example-app项目添加了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 ) ;
},
} );
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码(仅服务器端)时,我得到了
Exception while invoking method 'autoGenerate' Error: ENOENT,
no such file or directory '/Uses/martinfox/tmp/auto-generated/myFile.js'
Run Code Online (Sandbox Code Playgroud)
注意/使用/ martinfox/tmp /自动生成的文件夹确实存在