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

javascript - Getting Uncaught TypeError: fs.readdirSync is not a function when using Browserify with Nutritionix NodeJS client l

programmeradmin5浏览0评论

I'm trying to build a demo app using Node and I keep getting Uncaught TypeError: fs.readdirSync is not a function error when trying to use the Nutrionix NodeJS Client Library () and Browserify.

I'm following this tutorial / up to the Using the Browserify Output section but instead of of using Underscore and the code provided for main.js, I'm using the Nutritionix NodeJS client library and the following code in main.js:

var NutritionixClient = require('nutritionix');

var nutritionix = new NutritionixClient({
    appId: '7c710fbd',
    appKey: 'a2f106128aa4b2ab81fd783fca5bf0ee'
    // debug: true, // defaults to false
});

My HTML using Jade is the following:

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')

I use the following in the mand line to build a new JavaScript file with the Nutritionix:

browserify public/javascripts/main.js -o public/javascripts/nutri.js

When I run this locally, in the console, I get Uncaught TypeError: fs.readdirSync is not a function in line 465 of nutri.js, which is the file made by Browserify. That line is fourth line in the following function:

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;
}

I'm not sure why I keep getting this error. When I go through the Browserify tutorial which I linked at the top, I have no problem getting the Browserified file to work. Please let me know if more information is needed from me and any help is greatly appreciated.

I'm trying to build a demo app using Node and I keep getting Uncaught TypeError: fs.readdirSync is not a function error when trying to use the Nutrionix NodeJS Client Library (https://github./nutritionix/nodejs-client-library) and Browserify.

I'm following this tutorial http://www.sitepoint./getting-started-browserify/ up to the Using the Browserify Output section but instead of of using Underscore and the code provided for main.js, I'm using the Nutritionix NodeJS client library and the following code in main.js:

var NutritionixClient = require('nutritionix');

var nutritionix = new NutritionixClient({
    appId: '7c710fbd',
    appKey: 'a2f106128aa4b2ab81fd783fca5bf0ee'
    // debug: true, // defaults to false
});

My HTML using Jade is the following:

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')

I use the following in the mand line to build a new JavaScript file with the Nutritionix:

browserify public/javascripts/main.js -o public/javascripts/nutri.js

When I run this locally, in the console, I get Uncaught TypeError: fs.readdirSync is not a function in line 465 of nutri.js, which is the file made by Browserify. That line is fourth line in the following function:

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;
}

I'm not sure why I keep getting this error. When I go through the Browserify tutorial which I linked at the top, I have no problem getting the Browserified file to work. Please let me know if more information is needed from me and any help is greatly appreciated.

Share Improve this question asked Jan 12, 2016 at 4:58 alejandroj1234alejandroj1234 1411 gold badge1 silver badge7 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 2

Use brfs , https://github./browserify/brfs

fs.readFileSync() and fs.readFile() static asset browserify transform

This module is a plugin for browserify to parse the AST for fs.readFileSync() calls so that you can inline file contents into your bundles.

Even though this module is intended for use with browserify, nothing about it is particularly specific to browserify so it should be generally useful in other projects.

fs is a low-level API for filesystem operations, you can't browserifiy it just like that. You have to provide a replacement, this for example: https://github./mafintosh/browserify-fs. Since you can't access the client's filesystem from the browser, browserify-fs uses LevelDB to emulate a filesystem.

Although it is one of opinion...

First, Nutrionix NodeJS Client Library designed for use in Node.js runtime, instead use in browser(chrome, firefox, and others) runtime.

Maybe Client doesn't means browser, instead it means like who use Nutrionix by Node.js. So, error message is correct.

Second, ShanShan's suggestion is also correct in the case of like it, but in alejandroj1234's case, it seems not correct. Because browserify-fs not support readdirSync method.

For instance, I replace fs module and browserify-fs, but error message is still appear.(It use in lib-loader module, used by nutrionix module)

So my conclusion is, your way to use of browserify is correct, but it's library do not fit in case.

I used a line below. It helped me.

import * as fs from 'fs';

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论