RequireJS 模块名称“requirejs"尚未加载上下文.使用要求([]) [英] RequireJS Module name "requirejs" has not been loaded yet for context. use require([])

查看:107
本文介绍了RequireJS 模块名称“requirejs"尚未加载上下文.使用要求([])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一个模块没有加载的帮助页面,但是,这似乎是另一种情况,因为 requirejs 找不到 requirejs.

I know there is a help page for a module not loading, however, this seems like a different case due to the fact that requirejs cannot find requirejs.

我使用 npm 安装了所有依赖项.我的主脚本位于项目根目录中,由 html 页面调用.

I installed all of my dependencies using npm. My main script is in the project root, which is called by an html page.

这是网页的一个片段:

<script type="text/javascript" data-main="./airportLeafletScript.js" src="node_modules/requirejs/require.js"></script>

然后 airportLeafletScript.js 的头部看起来像这样:

And then the header of airportLeafletScript.js looks like this:

var requirejs = require('requirejs');

requirejs.config({
//Pass the top-level main.js/index.js require
//function to requirejs so that node modules
//are loaded relative to the top-level JS file.
nodeRequire: require
});

requirejs(['turf', 'jquery', 'leaflet', 'leaflet-rotatedmarker', 
    'leaflet-slider', 'shpjs', 'jquery-ui', 'json3', 'esri-leaflet'],
    function   (turf, $, L, Marker, SliderControl, shp) {
 //foo and bar are loaded according to requirejs
 //config, but if not found, then node's require
 //is used to load the module.
});

实际的错误信息是:

11:03:19.078 Error: Module name "requirejs" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
makeError() require.js:168
localRequire() require.js:1433
requirejs() require.js:1794
<anonymous> airportLeafletScript.js:46
1 require.js:168:17

那么我怎样才能让 requirejs 为这个项目工作呢?它似乎没有正确配置

So how can I get requirejs working for this project? It doesn't seem to be configured properly

推荐答案

你展示的代码 var requirejs = require('requirejs'); 是为了在 Node.js 中运行,而不是在浏览器中.还要注意传递给 requirejs.config 的配置,其中包括 nodeRequire,这仅在 Node.js 中有意义.

The code you are showing that does var requirejs = require('requirejs'); is meant to be run in Node.js, not in a browser. Also note the configuration passed to requirejs.config which includes nodeRequire, which only makes sense in Node.js.

当您使用显示的 script 标记在浏览器中加载该代码时,RequireJS 已经 加载,并且 require 调用会尝试重新加载它,无论你如何切割它都是糟糕的.它碰巧触发了特定于 RequireJS 的错误,但这是尝试通过 RequireJS 加载为 Node.js 编写的代码的副作用.

When you load that code in a browser with the script tag that you show, RequireJS is already loaded, and the require call tries to load it again, which is bad no matter how you cut it. It happens to trigger a RequireJS-specific error but that's a side-effect of trying to load code written for Node.js through RequireJS.

当您在 Node.js 中加载相同的代码时,require 调用由 Node.js 处理.它加载 RequireJS 然后你可以配置它并使用它.

When you load the same code in Node.js, the require call is handled by Node.js. It load RequireJS and then you can configure it and use it.

这篇关于RequireJS 模块名称“requirejs"尚未加载上下文.使用要求([])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆