简体   繁体   English

为什么收到“未捕获的ReferenceError:未定义要求”?

[英]Why I get “Uncaught ReferenceError: require is not defined”?

I want to use lolapi , so I went with my terminal to folder where project is and did npm install lolapi . 我想使用lolapi ,所以我将终端带到项目所在的文件夹,并且npm install lolapi

My code is a really simple cordova app: 我的代码是一个非常简单的cordova应用程序:

HTML: HTML:

<body>
    <div class="app">
        <h1>&nbsp;&nbsp;Lol App</h1>
    </div>

    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="js/lolJ.js"></script>
</body>

JavaScript (lolJ.js): JavaScript(lolJ.js):

var options = {
  useRedis: true,
  hostname: '127.0.0.1',
  port: 6379,
  cacheTTL: 7200
};

var lolapi = require('lolapi')('***-***-***-***-***', 'euw', options);

lolapi.setRateLimit(10, 500);

var summonerName = 'Wickd';
lolapi.Summoner.getByName(summonerName, function (error, summoner) {
  if (error) throw error;
  /* summoner object
  { Wickd:
    {
      id: 71500,
      name: 'Wickd',
      profileIconId: 613,
      summonerLevel: 30,
      revisionDate: 1408199475000
    }
  } */
  console.log(summoner);
  console.log(summoner[summonerName]);
  var summonerId = summoner[summonerName].id;
  lolapi.Summoner.getRunes(summonerId, function (error, runes) {
    if (error) throw error;
    // do something with runes
  })
});

I replaced Wickd with my lol username, but still nothing happens. 我用大声笑的用户名替换了Wickd ,但仍然没有任何反应。

As you can see I'm really new to this, so please, any guide will be great 如您所见,我真的很陌生,因此,任何指导都将是很棒的

Thanks 谢谢

The require syntax is what's referred to as CommonJS , the default way of how you load modules within Node.js environments. require语法是CommonJS ,它是在Node.js环境中加载模块的默认方式。

In order to use the require syntax in a browser environment you'd need to use tools like Browserify which handles this for you by bundling your application together with your dependencies. 为了在浏览器环境中使用require语法,您需要使用Browserify之类的工具,该工具通过将应用程序与依赖项捆绑在一起来为您处理此问题。

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

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