我正在尝试使用 Node 构建一个演示应用程序,但我不断收到未捕获的类型错误:fs.readdirSync 不是函数尝试使用 Nutrionix NodeJS 客户端库时出错(https://github.com/nutritionix/nodejs-client-library https://github.com/nutritionix/nodejs-client-library)和浏览器化。
我正在关注这个教程http://www.sitepoint.com/getting-started-browserify/ http://www.sitepoint.com/getting-started-browserify/至使用 Browserify 输出部分,但我没有使用 Underscore 和为 main.js 提供的代码,而是使用 Nutritionix NodeJS 客户端库和 main.js 中的以下代码:
var NutritionixClient = require('nutritionix');
var nutritionix = new NutritionixClient({
appId: '7c710fbd',
appKey: 'a2f106128aa4b2ab81fd783fca5bf0ee'
// debug: true, // defaults to false
});
我使用 Jade 的 HTML 如下:
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
script(src='javascripts/nutri.js')
script(src='javascripts/main.js')
我在命令行中使用以下命令通过 Nutritionix 构建新的 JavaScript 文件:
browserify public/javascripts/main.js -o public/javascripts/nutri.js
当我在控制台本地运行此命令时,我得到未捕获的类型错误:fs.readdirSync 不是函数Nutri.js 的第 465 行,这是由 Browserify 制作的文件。该行是以下函数中的第四行:
function readDirp(path, excludeList) {
var lib = {};
var expand = new Expander(path);
var files = fs.readdirSync(path).map(expand);
files.forEach(function(file){
if (excluded(file.name, excludeList) === false) {
lib[file.name.split('.').shift()] = require(file.location);
}
});
return lib;
}
我不确定为什么我不断收到此错误。当我浏览顶部链接的 Browserify 教程时,我可以毫无问题地让 Browserified 文件正常工作。如果需要我提供更多信息,请告诉我,我们将不胜感激。