javascript AngularJs ReferenceError:需要未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32751829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:42:53  来源:igfitidea点击:

AngularJs ReferenceError: require is not defined

javascriptangularjsnode.js

提问by celsomtrindade

I know there is other questions about this problem, but I've ready more than 10 answers and tried every single one and none of them worked for me.

我知道关于这个问题还有其他问题,但我已经准备了 10 多个答案并尝试了每一个,但没有一个对我有用。

I'm getting this error: Uncaught ReferenceError: require is not definedwhen i try to load an external plain javascript file to encode in JWT.

我收到此错误:Uncaught ReferenceError: require is not defined当我尝试加载外部纯 javascript 文件以在 JWT 中进行编码时。

This is the file I'm trying to include with require(): https://github.com/hokaccha/node-jwt-simpleI've download with npm.

这是我试图包含的文件require()https: //github.com/hokaccha/node-jwt-simple我用npm.

In my code I tried lots of things.

在我的代码中,我尝试了很多东西。

  • Include in the main.jswith grunt concact;
  • Load manually inside the <head>with the <script src="path/to/folder"></script>;
  • Install RequireJs with npm for nodeJs;
  • 包含在main.js与 grunt 的联系中;
  • <head><script src="path/to/folder"></script>;在里面手动加载
  • 使用 npm 为 nodeJs 安装 RequireJs;

With all of those attempts, I got the same errors. What am I doing wrong?

通过所有这些尝试,我遇到了相同的错误。我究竟做错了什么?

This is the code I'm using right now:

这是我现在使用的代码:

index.html

索引.html

<head>
<script type="text/javascript" src="dist/js/angular.min.js"></script>
<script type="text/javascript" src="dist/js/ui-router.min.js"></script>
<script type="text/javascript" src="dist/js/jwt.js"></script>
[... rest of head ...]

appCtrl.js(will concat later on the build with the rest of the app)

appCtrl.js(稍后将在构建中与应用程序的其余部分连接)

.controller('MainCtrl', 
    ['$rootScope', '$scope', '$state', 
    function($rootScope, $scope, $state) {
        var jwt = require('jwt');
        var payload = { foo: 'bar' };
        var secret = 'xxx';
        var token = jwt.encode(payload, secret);
        console.log(token);
}])

My main objective with this is to handle the user authentication based on a JWT token. The problem right now is to encode/decode the token when the user is login in into the app.

我的主要目标是处理基于 JWT 令牌的用户身份验证。现在的问题是在用户登录应用程序时对令牌进行编码/解码。

Even with all of these, I still get the error. Do you guys know how to fix this?

即使有了所有这些,我仍然收到错误消息。你们知道如何解决这个问题吗?

回答by Josh Beam

If you want to use requireon the client, you need to use something like Browserifyor webpack(I definitely recommend the latter).

如果要require在客户端使用,则需要使用Browserifywebpack 之类的东西(我绝对推荐后者)。

You're getting that error because, in fact, you have never defined requireanywhere on your client-side code. That JWT repo does not provide that functionality for you.

您收到该错误是因为实际上您从未require在客户端代码中定义任何地方。该 JWT 存储库不为您提供该功能。

Also, looking at the docs for the JWT repo you provided, it appears that it expects you to use it in a node.js environment (which provides the commonjs requirefor you).

此外,查看您提供的 JWT 存储库的文档,它似乎希望您在 node.js 环境(它require为您提供 commonjs )中使用它。

回答by Paul G

On the server side (ie server.js file), for node, use:

在服务器端(即 server.js 文件),对于节点,使用:

var jwt = require('jwt-simple');

Then, to encode:

然后,编码:

var secret = "Ieaticecreamforbreakfast";

and in your login functionality... once you check the login criteria

并在您的登录功能中...一旦您检查登录条件

var token = jwt.encode({id: ID, otherInfo: "can go here"}, secret); 

res.send({token: token});

You can then set the response token in as a header for additional http requests in your angular, frontend side. Therefore, whenever a user makes a request to the backend, you have access to their encoded ID in the JWT

然后,您可以将响应令牌设置为 angular 前端的其他 http 请求的标头。因此,每当用户向后端发出请求时,您都可以访问他们在 JWT 中的编码 ID