我是节点新手,请原谅我,如果这很简单..我现在在互联网上搜索了3天,并且找不到任何关于如何将Browserify与AngularJS和Gulp结合使用的示例。所以我不知道。
我正在开发一款应用,我遵循了guide。我喜欢{strong> mainCtrl.js 中的require('fs')
,并使用函数fs.readdirSync();
但是给了我一个typeError并返回fs.readdirSync不是函数,为什么不是'它有效吗?
TypeError: fs.readdirSync is not a function
at new module.exports (app.js:49)
at Object.invoke (app.js:9502)
at extend.instance (app.js:14347)
at nodeLinkFn (app.js:13464)
at compositeLinkFn (app.js:12896)
at compositeLinkFn (app.js:12899)
at compositeLinkFn (app.js:12899)
at compositeLinkFn (app.js:12899)
at publicLinkFn (app.js:12776)
at app.js:6649
我的代码在这里:
app.js
'use strict';
require('angular');
require('angular-route');
require('angular-animate');
var mainCtrl = require('./controllers/mainCtrl');
var app = angular.module('myApp', ['ngRoute', 'ngAnimate'])
app.controller('mainCtrl', ['$scope', '$http', mainCtrl]);
mainCtrl.js
'use strict';
module.exports = function($scope, $http) {
var fs = require('fs');
console.log("fs in mainCtrl: ", fs); // this console logs Object {}
var files = fs.readdirSync('data/');
console.log("files in mainCtrl: ", files);
};
Gulpfile.js
'use strict';
var gulp = require('gulp');
var browserify = require('browserify')
gulp.task('browserify', function() {
return browserify('./js/app.js')
.bundle()
.pipe(source('app.js'))
.pipe(gulp.dest('./build'));
答案 0 :(得分:2)
等等,您是否尝试在浏览器中使用文件系统函数?
不,你不能这样做。期。如果你考虑安全隐患,你就会明白为什么。