---
title: "seajs require(\"jquery\")返回null解决方法"
author: "heicx_sudo"
date: "2014-04-29T17:47:51Z"
tags: []
categories: ["Web前端"]
slug: "seajs-require-jquery-null"
draft: false
lastmod: "2014-04-29T17:48:37Z"
id: 2057191
url: "https://heicx.iteye.com/blog/2057191"
---

seajs.use 某具名模块时发现其引用为 null 的问题，或是移动了文件位置导致引用为 null 或者 object is not function 的问题。这些问题都指向 Sea.js 的一个基本约定原则：ID 和路径匹配原则。

    使用 seajs.use 或 require 进行引用的文件，如果是具名模块（即定义了 ID 的模块），会把 ID 和 seajs.use 的路径名进行匹配，如果一致，则正确执行模块返回结果。反之，则返回 null。

 

首先可以先参考这个issue：https://github.com/seajs/seajs/issues/962

 

由于在 Sea.js 中，所有 JavaScript 模块都遵循 CMD（Common Module Definition） 模块定义规范。该规范明确了模块的基本书写格式和基本交互规则。

jQuery 1.7开始支持AMD规范，可以和遵循AMD规范的脚本加载器协作，比如RequireJS或者curl.js。

 

所以需要调整JQuery源码，如下：

 

```
//old
if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
    define( "jquery", [], function () { return jQuery; } );
}

//new
if ( typeof define === "function" && define.cmd && define.cmd.jQuery ) {
    define( "jquery", [], function () { return jQuery; } );
}
```