简体   繁体   English

TypeError:无法在module.exports中设置undefined的属性

[英]TypeError: Cannot set property of undefined in module.exports

'use strict';
module.exports = function () {
    this.myVar = 'example';
    console.log(this.myVar);
};

I get the error: 我得到错误:

/home/karl/mymodule.js:3
    this.myVar = 'example';
               ^

TypeError: Cannot set property 'myVar' of undefined

In strict mode this is undefined . 在严格模式下, thisundefined But in non-strict mode this is nothing but pointing to GLOBAL . 但是在非严格模式下, this只不过指向GLOBAL So here you are trying to set something to undefined . 因此,在这里您尝试将某些内容设置为undefined

Where exactly were you intending to attach myVar ? 您打算将myVar固定在哪里? On your module ? 在您的module Or on GLOBAL ? 还是在GLOBAL If it's on GLOBAL (which is actually a bad idea), use GLOBAL.myVar or global.myVar . 如果在GLOBAL (这实际上是个坏主意),请使用GLOBAL.myVarglobal.myVar If it's on your module you could do module.exports.myVar = 'example'; 如果在您的模块上,则可以执行module.exports.myVar = 'example';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM