本文介绍了得到"ReferenceError:未定义jQuery".用于在npm上安装软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 jRCarousel jQuery插件发布到 npm .我最初收到的错误是名称不能包含大写字母,因此我在package.json中进行了更改,然后将其发布,但是在npm网站上尝试"Try it out"选项时,出现"ReferenceError:jQuery is未定义"错误.由于在package.json中指定了依赖项,因此不确定为什么会出错.

I have published a jRCarousel jQuery plugin to npm. I initially got the error that name can not contain capital letters, so I have changed that in package.json and then published it got published but on npm website when I try the "Try it out" option I get the "ReferenceError: jQuery is not defined" error. Not sure why is the error as I have specified the dependency in package.json.

如果仅对package.json进行了更改,而对package或模块中的其他文件没有任何更改,我如何仅在npm上更新package.json文件,或者我必须将其发布为新版本.(我要避免).

Also if there are any changes to only package.json and not to any other files in package or module , how can I update only the package.json file on npm, or do I have to publish it new version no.(which I want to avoid).

这是我的package.json

Here is my package.json

{
  "name": "jrcarousel",
  "version": "1.0.0",
  "description": "jRCarousel - jQuery Responsive Carousel,
                  a modern, lightweight responsive carousel plugin",
  "main": "dist/jRCarousel.min.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/vinayakjadhav/jRCarousel.git"
  },
  "keywords": [
    "jquery-plugin",
    "ecosystem:jquery",
    "jQuery Resposive Carousel",
    "jQuery slider",
    "Responsive Slider",
    "Carousel",
    "Slider",
    "Gallery",
    "lightweight"
  ],
  "author": "Vinayak Rangnathrao Jadhav",
  "license": "MIT",
  "dependencies": {
    "jquery": "^1.9.1"
  },
  "bugs": {
    "url": "https://github.com/vinayakjadhav/jRCarousel/issues"
  },
  "homepage": "https://github.com/vinayakjadhav/jRCarousel",
  "devDependencies": {}
}

npm软件包

Github存储库

尝试退出链接

注意:我是npm的新手,已经搜索了很多但与jquery-plugin发布有关的可用信息很少.感谢您的帮助.

Note: I am new to npm, have searched a lot but very less info available related to jquery-plugin publishing. Any help is appreciated.

推荐答案

使用此代码包装您的插件. UMD模式使您的插件可以在浏览器和大多数模块捆绑器中运行.

Wrap your plugin with this code. It's the UMD pattern that allows your plugin to run inside browser and most of module bundlers.

(function (root, factory) {
    if (typeof define === "function" && define.amd) {
        define(["jquery"], factory);
    } else if (typeof exports === "object") {
        module.exports = factory(require("jquery"));
    } else {
        factory(root.jQuery);
    }
}(this, function ($) {
    ...your plugin code goes here...
}));

这篇关于得到"ReferenceError:未定义jQuery".用于在npm上安装软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:54