将一个Javascript文件导入到REPL会话中
我在Windows 10 TP生成9926,使用nodejs。 我希望能够将一个Javascript导入到在Windows命令提示符下运行的nodejs的当前会话中,以便可以从REPL中调用导入的脚本中的函数。 我怎样才能做到这一点? 我的“导入”和“要求”的尝试没有成功。
我尝试了从具有“learn.js”javascript的目录运行的nodejs会话中的以下内容:
var n = require("learn.js")
然后得到以下错误信息:
Error: Cannot find module 'learn.js' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:278:25) at Module.require (module.js:365:17) at require (module.js:384:17) at repl:1:9 at REPLServer.defaultEval (repl.js:132:27) at bound (domain.js:254:14) at REPLServer.runBound [as eval] (domain.js:267:12) at REPLServer.<anonymous> (repl.js:279:12) at REPLServer.emit (events.js:107:17)
learn.js文件需要如下所示:
"use strict"; module.exports.myfunc1 = function() { console.log("Hi there."); } module.exports.myfunc2 = function() { console.log("Goodbye."); }
另外,你必须这样要求:
var n = require('./learn.js');
要么
var n = require('./learn');
您必须创build相对于您运行REPL的位置的path。 节点不会自动检查本地目录。