Nodejs对目录的非法操作

时间:2016-03-12 18:07:01

标签: node.js

我尝试使用markdown-pdf NPM包将markdown文件的文件夹编译成单个PDF。

我有一个简单的脚本来完成这项工作:

var mpdf = require('markdown-pdf');
var fs = require('fs');

var mDocs = fs.readdirSync('./understandinges6/manuscript/');
mDocs = mDocs.map(function(d) { return 'understandinges6/manuscript/' + d });

var Book = 'understandinges6.pdf';

mpdf().concat.from(mDocs).to(Book, function() {
    console.log("Created", Book);
});

但是当我执行脚本时,会出现此错误:

events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: EISDIR: illegal operation on a directory, read
    at Error (native)

这很奇怪,因为我在我的主文件夹中拥有相应的权限。我在脚本中指定了输出文件夹/文件,只是用fs.readdirSync读取。

对此有何想法?

2 个答案:

答案 0 :(得分:0)

mDocs = mDocs.map(function(d) { return 'understandinges6/manuscript/' + d });您忘记添加“./”。重写为mDocs = mDocs.map(function(d) { return './understandinges6/manuscript/' + d });

答案 1 :(得分:0)

很酷,我在这里遇到问题:

manuscripts/文件夹中有一个images/子文件夹,其中包含一些png。当脚本试图将images/.md读取并转换为.pdf时,错误就会被触发。

这是带有images / inside的数组:

[ 'understandinges6/manuscript/00-Introduction.md',
  'understandinges6/manuscript/01-Block-Bindings.md',
  'understandinges6/manuscript/02-Strings-and-Regular-Expressions.md',
  'understandinges6/manuscript/03-Functions.md',
  'understandinges6/manuscript/04-Objects.md',
  'understandinges6/manuscript/05-Destructuring.md',
  'understandinges6/manuscript/06-Symbols.md',
  'understandinges6/manuscript/07-Sets-And-Maps.md',
  'understandinges6/manuscript/08-Iterators-And-Generators.md',
  'understandinges6/manuscript/09-Classes.md',
  'understandinges6/manuscript/10-Arrays.md',
  'understandinges6/manuscript/11-Promises.md',
  'understandinges6/manuscript/12-Proxies-and-Reflection.md',
  'understandinges6/manuscript/13-Modules.md',
  'understandinges6/manuscript/A-Other-Changes.md',
  'understandinges6/manuscript/B-ECMAScript-7.md',
  'understandinges6/manuscript/Book.txt',
  'understandinges6/manuscript/images' ]

解决方案?只需pop() mDocs数组(现在只是docs):

var mpdf = require('markdown-pdf');
var fs = require('fs');

var mDocs = fs.readdirSync('understandinges6/manuscript/');
var docs = mDocs.map(function(d) { return 'understandinges6/manuscript/' + d });

docs.pop();

var Book = 'understandinges6.pdf';

mpdf().concat.from(docs).to(Book, function() {
    console.log("Created", Book);
});