获取“TypeError:调用`npm start`时无法读取未定义的属性”文件名“

我正在关注这个相当冗长但非常丰富的post,关于使用以下技术堆栈设置Web应用程序:

  • es 6
  • 节点+expression
  • 车把
  • 反应+反应路由器
  • webpack + babel

示例代码可以在这里find。

我一直在跟着,一切都很顺利,我理解了最重要的概念。 但是,当我尝试运行它时(使用git clone下载之后)出现错误,如下所示:

 $ npm install $ npm start 

生成的输出包括错误是:

 > react-ssr-example@0.0.1 start /Users/nburk/Developer/node/templates/react-universal-web-apps-simple > npm-run-all --parallel gulp server > react-ssr-example@0.0.1 server /Users/nburk/Developer/node/templates/react-universal-web-apps-simple > node-dev app/babel-server.js > react-ssr-example@0.0.1 gulp /Users/nburk/Developer/node/templates/react-universal-web-apps-simple > gulp [19:10:39] Using gulpfile ~/Developer/node/templates/react-universal-web-apps-simple/gulpfile.js [19:10:39] Starting 'build:scss'... [19:10:39] Starting 'build:watch:app'... TypeError: Cannot read property 'filename' of undefined at Object.obj.(anonymous function) [as runInThisContext] (/Users/nburk/Developer/node/templates/react-universal-web-apps-simple/node_modules/node-dev/lib/hook.js:25:55) at Object.<anonymous> (/Users/nburk/Developer/node/templates/react-universal-web-apps-simple/node_modules/graceful-fs/fs.js:10:13) at Module._compile (module.js:425:26) at Module._extensions..js (module.js:432:10) at nodeDevHook (/Users/nburk/Developer/node/templates/react-universal-web-apps-simple/node_modules/node-dev/lib/hook.js:58:7) at require.extensions.(anonymous function) (/Users/nburk/Developer/node/templates/react-universal-web-apps-simple/node_modules/babel-core/lib/api/register/node.js:214:7) at Object.nodeDevHook [as .js] (/Users/nburk/Developer/node/templates/react-universal-web-apps-simple/node_modules/node-dev/lib/hook.js:58:7) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12) at Module.require (module.js:366:17) at require (module.js:385:17) [19:10:39] Finished 'build:watch:app' after 12 ms [19:10:39] Starting 'lint:app'... [ERROR] 19:10:39 TypeError [19:10:39] Starting 'server'... 

有没有人看到我失踪? 我甚至不知道从哪里开始,错误中的文件名并不能告诉我任何东西。

更新

我检查了发生错误的文件( /node_modules/node-dev/lib/hook.js ),下面是导致它的代码:

 /** * Patch the specified method to watch the file at the given argument * index. */ function patch(obj, method, optionsArgIndex) { var orig = obj[method]; if (!orig) return; obj[method] = function () { var opts = arguments[optionsArgIndex]; var file = typeof opts == 'string' ? opts : opts.filename; if (file) callback(file); return orig.apply(this, arguments); }; } 

你可以简单地通过这样做来显式强制节点的开发版本来解决这个问题:

 npm i node-dev@3.0.0 --save-dev 

修复#130 https://github.com/fgnass/node-dev/issues/130带来了@rramakrishnaa错误提到的其他一些&#x3002;

您正在获取该错误,因为您正试图访问其值未定义的variables的属性。 在你的情况, optsvar opts = arguments[optionsArgIndex]; 这一行,你不能访问一个未定义的variables的属性。

将其封装在try-catch中或在访问对象的嵌套属性之前添加检查。 在JavaScript中通常被认为是一个糟糕的方法来直接访问嵌套variables。

例如,而不是var a = bcd; 你应该使用var a = (a && ab && abc) || <some default value>; var a = (a && ab && abc) || <some default value>;

在第一种情况下,如果bc未定义,则您的应用程序将崩溃,但是,如果bc在第二种情况下未定义,则会为您指定a的默认值