使用requireJS加载模块的时候,有时候会碰到如下的错误:

Uncaught Error: Module name "module1" has not been loaded yet for context: _. Use require([])

比如下面的代码就会报这个错误:

require([], function() {var module = require("module1");alert(module.name);
});

这个错误在requireJS官网上写的很明白:
This occurs when there is a require('name') call, but the 'name' module has not been loaded yet.

我们先来看下,requireJS中定义模块和加载模块的标准方式:

// 加载模块的标准方式
require(['foo','jquery'], function (foo,$) {//foo is now loaded.
});// 定义模块的标准方式
define(['module1', 'module2'], function(m1, m2) {return {method: function() {m1.methodA();m2.methodB();}};});

如果我们需要加载的或者定义的模块比较少,这种标准的写法是很清晰的。

但是如果我们需要加载的模块很多,那么这种一一对应的写法很繁琐。

define(['dep1', 'dep2', 'dep3', 'dep4', 'dep5', 'dep6', 'dep7', 'dep8'],function(dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8){...}
);

为了解决这个问题,我们可以使用以下2种方式来定义模块:

方式1:If you are using the simplified define wrapper, make sure you have require as the first argument to the definition function

define(function (require) {var dep1 = require('dep1'),dep2 = require('dep2'),dep3 = require('dep3'),dep4 = require('dep4'),dep5 = require('dep5'),dep6 = require('dep6'),dep7 = require('dep7'),dep8 = require('dep8');}
});

方式2: If you are listing dependencies in the dependency array, make sure that require and name are in the dependency array

define(['require', 'dep1', 'dep2', 'dep3', 'dep4', 'dep5'], function (require) {var dep1 = require('dep1');var dep2 = require('dep2');
});

但是下面的这种写法就不行,会报错HAS NOT BEEN LOADED YET FOR CONTEXT

//THIS WILL FAIL
define(['require'], function (require) {var namedModule = require('name');
});

官网上的解释是:

This fails because requirejs needs to be sure to load and execute all dependencies before calling the factory function above. If a dependency array is given to define(), then requirejs assumes that all dependencies are listed in that array, and it will not scan the factory function for other dependencies. So, either do not pass in the dependency array, or if using the dependency array, list all the dependencies in it.

最后官网上特别强调:require('name')这种写法,只应该出现在define()或者require()的回调函数中。

Be sure that require('name') only occurs inside a define() definition function or a require() callback function, never in the global space by its own.

可以看到使用define()定义模块的时候,如果依赖的模块比较少,那么可以使用标准方式;如果依赖的模块很多,那么可以使用方式1或者方式2来解决。很显然,使用require()加载模块的时候,也存在和define()一样的问题。经过我的试验:使用方式2也是可以的。

方式3:使用require加载多个模块的时候

//异步加载module1模块,加载完成后调用回调函数
require(["module3","module1","module2"], function() {var m1 = require("module1");alert(m1.name);
});

总结:使用define()定义模块,使用require()加载模块,可以使用标准方式,或者是方式1,方式2,方式3,这样就能够实现requireJS中模块的正确加载和定义。

理解和解决requireJS的报错:MODULE NAME HAS NOT BEEN LOADED YET FOR CONTEXT相关推荐

  1. iOS 解决报错 Module file /Users/anmo/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/3FOJ1MTM...

    报错 Module file '/Users/anmo/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/3FOJ1MTM36H3U/Co ...

  2. npm 报错 Module build failed: Error: No PostCSS Config found in:

    npm 报错 Module build failed: Error: No PostCSS Config found in: 你在网上找的解决方案应该有提到webpack版本的问题.不要以为升级web ...

  3. 微信小程序报错module “wxParse/wxParse.js“ is not defined

    微信小程序报错module "wxParse/wxParse.js" is not defined 解决 在本地设置把 ES6 转 ES5 勾上就 可以了

  4. webpack4 报错 Module not found: Error: Can't resolve

    webpack 要打包的文件的路径 -o 打包好的输出文件的路径 报错 Module not found: Error: Can't resolve... 查到的解决方法通常是说 文件路径大小写写错了 ...

  5. Eclipse中更改JDK版本,解决ant编译报错的问题

    要改一个JDK版本 主要是为了解决ant编译报错,JDK版本不一致的问题. 1. Windows--Preferences--Java--Compiler(配置的为1.8)--Installed JR ...

  6. 解决fitz模块报错

    解决fitz模块报错 使用Python处理PDF文档时,需要使用fitz模块.由于Python 3.8与fitz有兼容问题,会出现以下错误信息: [省略部分错误信息] RuntimeError: Di ...

  7. 解决MySQL8.0报错:Unknown system variable 'validate_password_policy'

    解决MySQL8.0报错:Unknown system variable 'validate_password_policy'   一.问题描述 1.在安装MySQL8.0时,修改临时密码,因密码过于 ...

  8. 成功解决调用密钥报错4001 加密方式错误

    成功解决调用密钥报错4001 加密方式错误 目录 解决问题 解决思路 解决方法 解决问题 成功解决调用密钥报错4001 加密方式错误 解决思路 秘钥没有打开,会出现这样的错误! 解决方法 关掉密钥按钮 ...

  9. maven引用servlet_解决Maven 项目报错 java.httpservlet和synchronized使用方法

    使用java8 的lanmbe表达式时,使用java1.8编译,则会报错 需要在pom.xml的中添加 org.apache.maven.plugins maven-compiler-plugin 2 ...

  10. linux安装python库报错pywin32_完美解决pyinstaller打包报错找不到依赖pypiwin32或pywin32-ctypes的错误...

    报错信息 最近闲来无事,用python的tkinter库开发了一款带日程提醒的万年历桌面程序.在程序开发结束开始打包时,却发现一直报错 PyInstaller cannot check for ass ...

最新文章

  1. Linux-Ubuntu操作记录
  2. Git学习系列(二)创建本地仓库及文件操作
  3. WebMessenger完善后将作为一个开源项目
  4. 【luogu2737】 [USACO4.1]麦香牛块Beef McNuggets [动态规划 完全背包][数学 扩展欧几里德]...
  5. python清空字典保留变量_python彻底清除字典数据,clear方法使用
  6. sqlmap代理池_sqlmap +外部代理池绕过IP拦截
  7. Java——遍历List过程中添加和删除元素的问题(亲测第二种)
  8. virt-v2v 使用指南
  9. Planar Homography (共面点成像) Epipolar Geometry(对极几何)
  10. 蓝桥杯-代码-数字三角形
  11. 数据在内存中的大小端模式存储
  12. 检查丢失的软件包并安装它们的优雅方法?
  13. 线程同步(互斥锁与信号量的作用与区别)
  14. Windows10安装sql2016配置iis问题
  15. 【R语言】logistic回归(二)多因素logistic回归
  16. 集中化Linux日志管理系统
  17. 中国区块链专利数量全球第一 BAT专利数稳居世界20强
  18. android flutter教程,Android开发 学习Flutter 入门
  19. BLE - 连接时触发配对
  20. 32单片机与迪文屏通信的开发学习

热门文章

  1. 80 after generation to marry or not to marry is a question
  2. 同步和提交AOKP源码
  3. 干掉hao123恶意植入浏览器
  4. linux操作系统期末试卷及答案,Linux操作系统期末复习题(含答案).pdf
  5. canvas的width和style.width的区别
  6. 去哪下载python项目_Python 项目实践二(下载数据)第三篇
  7. 项目系统设计和数据库设计(追光的人)
  8. 使用RestTemplate  对接微信付款码支付接入问题记录
  9. java forward怎么用_java中forward是什么
  10. 修订歌词《仙剑》电视原声插曲《逍遥叹》