【问题标题】:Can this npm package be imported?这个 npm 包可以导入吗?
【发布时间】:2018-10-17 23:24:05
【问题描述】:

您好,我正在尝试将一个名为“jquery.terminal”的 npm 包导入到我的项目中。

我正在通过命令行使用 browserify 来转换 require("jquery.terminal");解释器将语句转换成可读的代码。

这是我当前的代码。

require("jquery.terminal");

jQuery(function($, undefined) {
    $('#term_demo').terminal(function(command) {
        if (command !== '') {
            try {
                var result = window.eval(command);
                if (result !== undefined) {
                    this.echo(new String(result));
                }
            } catch(e) {
                this.error(new String(e));
            }
        } else {
           this.echo('');
        }
    }, {
        greetings: 'JavaScript Interpreter',
        name: 'js_demo',
        height: 200,
        prompt: 'js> '
    });
});

这是 npm https://github.com/jcubic/jquery.terminal 上的包

这是我的错误

jQuery.Deferred exception: $(...).terminal is not a function TypeError: $(...).terminal is not a function
    at HTMLDocument.<anonymous> (file:///C:/Dev/term-game/bundle.js:6:21)
    at mightThrow (file:///C:/Dev/term-game/bundle.js:11049:29)
    at process (file:///C:/Dev/term-game/bundle.js:11117:12) undefined
jQuery.Deferred.exceptionHook @ bundle.js:11333
process @ bundle.js:11121
setTimeout (async)
(anonymous) @ bundle.js:11155
fire @ bundle.js:10783
fireWith @ bundle.js:10913
fire @ bundle.js:10921
fire @ bundle.js:10783
fireWith @ bundle.js:10913
ready @ bundle.js:11393
completed @ bundle.js:11403
bundle.js:6 Uncaught TypeError: $(...).terminal is not a function
    at HTMLDocument.<anonymous> (bundle.js:6)
    at mightThrow (bundle.js:11049)
    at process (bundle.js:11117)

【问题讨论】:

  • 你遇到了什么错误?
  • 我更新了 m 问题以包含控制台错误
  • 您是否尝试过先将 jQuery 添加到全局对象中? global.jQuery = require("jquery") 然后需要终端插件?

标签: jquery npm ecmascript-6 browserify jquery-terminal


【解决方案1】:

你需要包含 jQuery,这样你就有了 jQuery 对象,然后 jQuery 终端还使用 UMD jQuery 终端使用 require 来调用一个函数并将所有依赖项传递给它,在这种情况下它只是 jQuery。

var $ = require('jquery');
require('jquery.terminal')($);

$('#term').terminal();

【讨论】:

  • 这不起作用,仍然收到错误。该软件包正在导出为 UMD 模块,因此它应该可以工作,但我仍然遇到 browserify 问题。有替代品吗?
  • @TheHoop 我已经更新了执行插件返回的函数并将依赖项传递给它所需的答案,这就是 UMD 的工作方式。如果你想在包中包含 css 文件,你可以将 webpack 与 css + 样式加载器一起使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 2022-01-24
  • 2014-04-17
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
相关资源
最近更新 更多