如何检测我的Node.JS文件是否使用SH:node path-to-file或JS 调用require('path-to-file')?
这是Node.JS等同于我在Perl中的上一个问题:如果它没有加载require,我怎么能运行我的Perl脚本?
我刚刚开始使用node.js,我有一些Python的经验.在Python中,我可以检查__name__变量是否设置为"__main__",如果是,我知道我的脚本是直接运行的.在这种情况下,我可以运行测试代码或直接以其他方式使用模块.
node.js中有类似的东西吗?
为什么运行下面显示的代码会出错?(它用于Coursera的Stanford'Startup engineering'课程中的一个作业:https://class.coursera.org/startup-001/quiz/attempt ? quiz_id = 149 )
这个课程是在2013年6月 - 6月进行的,所以可能有节点或csv的更新可能会打破脚本吗?作业不是关于修复脚本,所以这个问题不是'作弊',而且课程目前还没有运行..
因此,环境是:Ubuntu 14.04(内核3-13-0-29-通用),节点v0.11.13,npm v1.4.9
我npm install在主目录中有'csv,accounting util和restler,脚本也在那里.
这让我完全难过...... :-(
错误信息:
[ubuntu@ip-xxx-xxx-xxx-xxx:~]$node market-research.js
Invoked at command line.
Wrote market-research.csv
Symbol Name Market Cap Previous Close Price P/E Ratio Shares EPS Earnings
/home/ubuntu/market-research.js:47
csv()
^
TypeError: object is not a function
at csv2console (/home/ubuntu/market-research.js:47:5)
at Request.response2console (/home/ubuntu/market-research.js:65:13)
at Request.EventEmitter.emit (events.js:110:17)
at Request.mixin._fireSuccess (/home/ubuntu/node_modules/restler/lib/restler.js:226:10)
at /home/ubuntu/node_modules/restler/lib/restler.js:158:20
at IncomingMessage.parsers.auto (/home/ubuntu/node_modules/restler/lib/restler.js:394:7)
at Request.mixin._encode (/home/ubuntu/node_modules/restler/lib/restler.js:195:29)
at /home/ubuntu/node_modules/restler/lib/restler.js:154:16
at Request.mixin._decode (/home/ubuntu/node_modules/restler/lib/restler.js:170:7)
at IncomingMessage.<anonymous> (/home/ubuntu/node_modules/restler/lib/restler.js:147:14) …Run Code Online (Sandbox Code Playgroud) 是否可以检查JavaScript文件是否正在直接运行,或者是否需要将其作为es6模块导入的一部分。
例如,包含一个主脚本。
// main.js
import './other';
if (mainTest){
console.log('This should run');
}
Run Code Online (Sandbox Code Playgroud)
导入依赖项。
// other.js
if (mainTest){
console.log('This should never run');
}
Run Code Online (Sandbox Code Playgroud)
包括<script src=main.js></script>应该导致来自main.jsother.js 的控制台消息,但不会导致。