2015-12-18 149 views
-5

是否可以在nodejs腳本中包含JavaScript文件以便能夠使用命令「node script.js」對其進行測試?在node.js腳本中包含JavaScript文件

//編輯 我一直在使用 「browserify的script.js> file.js」 生成的browserify文件,我的劇本的NodeJS不只是一個 「的console.log()」,我現在需要測試我的文件。我與browserify如何使用Node.js不是瀏覽器產生的JS,這是我的代碼,我想從node.js的腳本來測試:

executeGeneratePNG= function(sBlob) { 
var _generatePNG = function(){ 
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ 
// shim for using process in browser 
var process = module.exports = {}; 
var queue = []; 
var draining = false; 
var currentQueue; 
var queueIndex = -1; 

function cleanUpNextTick() { 
    draining = false; 
    if (currentQueue.length) { 
     queue = currentQueue.concat(queue); 
    } else { 
     queueIndex = -1; 
    } 
    if (queue.length) { 
     drainQueue(); 
    } 
} 

function drainQueue() { 
    if (draining) { 
     return; 
    } 
    var timeout = setTimeout(cleanUpNextTick); 
    draining = true; 

    var len = queue.length; 
    while(len) { 
     currentQueue = queue; 
     queue = []; 
     while (++queueIndex < len) { 
      if (currentQueue) { 
       currentQueue[queueIndex].run(); 
      } 
     } 
     queueIndex = -1; 
     len = queue.length; 
    } 
    currentQueue = null; 
    draining = false; 
    clearTimeout(timeout); 
} 

process.nextTick = function (fun) { 
    var args = new Array(arguments.length - 1); 
    if (arguments.length > 1) { 
     for (var i = 1; i < arguments.length; i++) { 
      args[i - 1] = arguments[i]; 
     } 
    } 
    queue.push(new Item(fun, args)); 
    if (queue.length === 1 && !draining) { 
     setTimeout(drainQueue, 0); 
    } 
}; 

// v8 likes predictible objects 
function Item(fun, array) { 
    this.fun = fun; 
    this.array = array; 
} 
Item.prototype.run = function() { 
    this.fun.apply(null, this.array); 
}; 
process.title = 'browser'; 
process.browser = true; 
process.env = {}; 
process.argv = []; 
process.version = ''; // empty string to avoid regexp issues 
process.versions = {}; 

function noop() {} 

process.on = noop; 
process.addListener = noop; 
process.once = noop; 
process.off = noop; 
process.removeListener = noop; 
process.removeAllListeners = noop; 
process.emit = noop; 

process.binding = function (name) { 
    throw new Error('process.binding is not supported'); 
}; 

process.cwd = function() { return '/' }; 
process.chdir = function (dir) { 
    throw new Error('process.chdir is not supported'); 
}; 
process.umask = function() { return 0; }; 

},{}],2:[function(require,module,exports){ 
(function (process){ 
console.log("-------blob-------"+sBlob); 
}).call(this,require('_process')) 
},{"_process":1}]},{},[2]); 

} 
return {generatePNG :_generatePNG} 
}; 
+0

該文件的共享空間? –

+0

[在Node.js中,我如何從我的其他文件中「包含」函數?](http://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-功能從我的其他文件) –

回答

4

JavaScript文件應包含ASM style modules然後可以使用require被包括在內。

var myModule = require("./my-module.js"); 
+0

@JitendraKhatri - 這就是這個答案的第一句話的前半部分說。 – Quentin

0

您應該使用require()。在Node.js中檢查出Modules API

var module = require("./script.js");