定义未定义 Javascript 节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28450263/
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 09:00:09  来源:igfitidea点击:

define is not defined Javascript Node

javascriptjquerynode.jsrequirejsrequire

提问by leaflet757

I am trying to run a Javascript file with Node to post blog updates to Tumblr.

我正在尝试使用 Node 运行 Javascript 文件以将博客更新发布到 Tumblr。

So far in my main.js file I have this:

到目前为止,在我的 main.js 文件中,我有这个:

// Tumblr Information
var tumblr = require('./vendor/tumblr');
tumblr.request(require('request'));
var Blog;

var jq = require('./vendor/jquery-1.11.1.min');
var reqq = require('./vendor/require');
var inher = require('./vendor/inheritance');

var grammars = require('./tracery/grammar');

But then I receive the following error when I run node main.js

但是当我运行 node main.js 时收到以下错误

ReferenceError: define is not defined
    at Object.<anonymous> (B:\Documents\Google Drive\Programming\CMPM 147 Tracery\Tumblr Tracery\js\
tracery\grammar.js:6:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (B:\Documents\Google Drive\Programming\CMPM 147 Tracery\Tumblr Tracery\js\
main.js:11:16)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)

And here is where the error is being thrown in the grammar.js file:

这是在grammar.js 文件中抛出错误的地方:

define(["./modifiers", "./node"], function(universalModifiers, Node) {'use strict';

    // other stuff...
});

I do not think a module is being loaded properly because it is stating the define function isn't defined. I believe this function comes from requireJS but I don't think I am loading it properly. Does anyone have any suggestions on why define might not be defined, or suggestions on how to load the correct module?

我不认为模块被正确加载,因为它表明未定义定义函数。我相信这个函数来自 requireJS,但我认为我没有正确加载它。有没有人对为什么不定义定义有任何建议,或者关于如何加载正确模块的建议?

Thank you.

谢谢你。

回答by Louis

It looks like you are trying to load a library that is coded according to the AMD (Asynchronous Module Definition) pattern, which Node does not support natively. It is perfectly possible for the author of a library to write such library so that it can be loaded in Node. The users of the library have to extend Node's module loading capabilities to understand AMDin order to load such library.

看起来您正在尝试加载根据 AMD(异步模块定义)模式编码的库,Node 本身不支持该模式。库的作者完全有可能编写这样的库以便它可以在 Node.js 中加载。该库的用户必须扩展 Node 的模块加载功能以了解 AMD才能加载此类库。

There are multiple loaders you can use, the one I use is amd-loader. Once installed with npm, you just add

您可以使用多种加载程序,我使用的是amd-loader。安装后npm,您只需添加

require("amd-loader");

before you load any AMD module.

在加载任何 AMD 模块之前。