[英]command line node.js; running script with modules
我正在使用node.js和幾個模塊\\庫進行測試。 為簡單起見,我將重點放在underscore.js上。
當我使用以下源代碼運行node.exe時:
require("./underscore.js");
_.each({one : 1, two : 2, three : 3}, function(num, key){ console.log(num); });
我明白了:
C:\Dropbox\personal-work\foo\test code>node library-tests.js
node.js:208
throw e; // process.nextTick error, or 'error' event on first tick
^
ReferenceError: _ is not defined
at Object.<anonymous> (C:\Dropbox\personal-work\foo\test code\library-tests.js:2:1)
at Module._compile (module.js:425:26)
at Object..js (module.js:443:10)
at Module.load (module.js:344:31)
at Function._load (module.js:303:12)
at Array.<anonymous> (module.js:463:10)
at EventEmitter._tickCallback (node.js:200:26)
同樣奇怪的是,當我像這樣運行它時:
node underscore.js library-tests.js
它似乎什么也沒做...我什至還添加了日志語句,它們似乎沒有執行。
我也嘗試將underscore.js源代碼粘貼到源代碼的頂部,並且遇到相同的錯誤...
有誰知道我在這里做錯了什么? 謝謝。
嘗試分配它:
var _ = require('../underscore.js');
您可以在帶注釋的源代碼中看到,如果下划線將在CommonJS實現中運行,則下划線不會將其自身添加到全局名稱空間中(Node.JS是其中之一)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.