繁体   English   中英

未捕获的 ReferenceError 要求未定义

[英]Uncaught ReferenceError require is not defined

我正在尝试制作一个随机数生成器,它接受字符串并将它们转换为从 0 到 1 的浮点数。我遇到了太多的“require is not defined”错误。 我已经使用“npm i chance”安装了“chance.js”,但没有任何变化。 我试过用谷歌搜索它并安装 browsify 但没有任何改变。 这是我的代码:

const ChanceJS = ("chance");
const serverSeed = "n$LKb@nLn1R*Eg7X";
const randomSeed = "g5gprgva13jfby2j";
const mod = ${serverSeed}-${randomSeed};

const chance = new ChanceJS(mod);
const ticket = chance.floating({min: 0, max: 1, fixed: 15});

console.log(ticket);

如果您需要更多信息来帮助我,请随时提问。

当你这样写时const ChanceJS = ("chance"); , 它等于const ChanceJS = "chance"; .
在这种情况下ChanceJS是字符串。

require function 只能在NodeJS中使用,不能直接在浏览器中使用

据我了解,您应该在 html 中添加以下script标签

<script src="http://chancejs.com/chance.min.js"></script>

之后你可以像这样使用 ChanseJS

const myChance = new Chance();

不需要任何require :)

暂无
暂无

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

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