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

Javascript 获得;TypeError:无法读取属性';文件名';“未定义”的定义;调用'npm start'时`,javascript,node.js,express,reactjs,webpack,Javascript,Node.js,Express,Reactjs,Webpack,我将介绍如何使用以下技术堆栈设置web应用程序: es 6 node+express 车把 反应路由器 网页+巴别塔 可以找到示例代码 我一直在跟进,进展很顺利,我确实理解了最重要的概念。但是,当我尝试运行它时(在我使用git clone下载它之后),我遇到了一个错误,如下所示: 生成的输出(包括错误)为: > react-ssr-example@0.0.1 start /Users/nburk/Developer/node/templates/react-universal-web-

我将介绍如何使用以下技术堆栈设置web应用程序:

  • es 6
  • node+express
  • 车把
  • 反应路由器
  • 网页+巴别塔
可以找到示例代码

我一直在跟进,进展很顺利,我确实理解了最重要的概念。但是,当我尝试运行它时(在我使用
git clone
下载它之后),我遇到了一个错误,如下所示:

生成的输出(包括错误)为:

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

出现该错误是因为您试图访问一个值未定义的变量的属性。在您的例子中,
opts
var opts=arguments[optionsArgIndex]获取值
undefined
此行,您无法访问未定义变量的属性

在访问对象的嵌套属性之前,请在try-catch或add检查中对此进行包装。在JavaScript中,直接访问嵌套变量通常被认为是一种不好的方法

例如,代替
var a=b.c.d应该使用
var a=(a&&a.b&&a.b.c)|


在第一种情况下,如果
b
c
未定义,您的应用程序将崩溃,但是,如果
b
c
在第二种情况下未定义,
a
将被分配您提供的默认值

您可以通过执行以下操作,显式强制使用以前版本的node dev来解决此问题:

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

修复#130带来了@rramakrishnaa bugs提到的一些其他问题。

谢谢!您知道为什么
opts
可能没有此属性吗?您知道谁在调用
修补程序吗?
?在这种特殊情况下,特定节点模块似乎没有处理异常。您必须检查模块预期的内容以及在参数中提供的内容。仔细阅读本模块的文档。错误发生在构建过程中,因此出错代码很可能在gulpfile.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