我已经在NodeJS项目中安装了DateJS,以便轻松处理添加的月份。我将其添加到package.json中。

 "datejs": "*"

然后通过安装:
root@v-hj-0190:~/deepak/appJade# npm -d install
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info preinstall [email protected]
npm info trying registry request attempt 1 at 15:08:34
npm http GET https://registry.npmjs.org/datejs
npm http 304 https://registry.npmjs.org/datejs
npm info install [email protected] into /root/deepak/appJade
npm info installOne [email protected]
npm info preinstall [email protected]
npm info build /root/deepak/appJade/node_modules/datejs
npm info linkStuff [email protected]
npm info install [email protected]
npm info postinstall [email protected]
npm info build /root/deepak/appJade
npm info linkStuff [email protected]
npm info install [email protected]
npm info postinstall [email protected]
npm info prepublish [email protected]
[email protected] node_modules/datejs
npm info ok
root@v-hj-0190:~/deepak/appJade#

我在app.js中添加了require行
...
var bodyParser = require('body-parser');
var Date = require('datejs');
...

但是以下代码仍给出错误:
var approvalDate =  Date.today();
                                 ^
TypeError: Object function Date() { [native code] } has no method 'today'
..


var n = 6;
console.log(n.months().fromNow());
                      ^
TypeError: Object 6 has no method 'months'

注意:我是NodeJS的新手,并且需要示例如何在项目中集成/使用DateJS的示例。到处都只提供了直接功能。

最佳答案

datejs通过扩展Date中内置的javascript来工作,因此无需将require分配给变量。代替..

var Date = require('datejs');

只需执行要求:
require('datejs')

然后全部设置完毕(或者,如果您出于某种原因将模块分配给变量,请使用“日期”以外的名称)。

09-18 11:35