简体   繁体   中英

Can't access Imported Node Module from within Node stdin

Why can't I access this?

someFile.js

const Context = require('./Context');

    const play = function(){
      process.openStdin().on('data', function(res) {
         if(Context.move(res, X)){ // I get an error here saying Context is undefined
            ... rest of the code
          }
   }

I'd like to not have to modify the behavior, the code in play as much as possible.

If you're in any kind of modern Node, preserve your context with an arrow function:

const Context = require('./Context');

const play = () => {
  process.openStdin().on('data', res => {
     if(Context.move(res, X)){
        ... rest of the code
     }
  });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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