这个npm包可以导入吗?

时间:2018-05-08 05:20:01

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

您好我正在尝试将名为“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)

1 个答案:

答案 0 :(得分:0)

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

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

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