如何导入非node.js文件? [英] How do you import non-node.js files?

查看:84
本文介绍了如何导入非node.js文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何加载不符合node.js格式的外部js文件。我正在尝试导入json序列化库。我怎么能这样做?

How do I load external js files that don't fit the node.js format. I am trying to import the json serialize library. How can I do this?

推荐答案

2个答案......

2 answers...

1)JSON对象内置于node.js,因此您只需调用JSON.parse()和JSON.stringify(),就不需要为此特定情况导入外部代码。

1) the JSON object is built-in to node.js, so you can just call JSON.parse() and JSON.stringify(), there is no need to import external code for this particular case.

2)导入外部代码,node.js遵循CommonJS模块规范,你可以使用require()

2) to import external code, node.js follows the CommonJS module specification and you can use require()

所以如果你有一个名为external.js的文件(与你的其余代码在同一目录中):

so if you have a file called external.js (in the same directory as the rest of your code):

this.hi = function(x){ console.log("hi " + x); }

并从你做的节点:

var foo = require("./external");
foo.hi("there");

你会看到输出 hi那里

这篇关于如何导入非node.js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆