Javascript 未正确导出使用RequireJS的模块
我目前正在开发一个本地JavaScript应用程序,它将帮助我为特定客户动态生成一些管理文档,如未付服务的发票和凭证 我将使用node_模块,到目前为止我使用的是Javascript 未正确导出使用RequireJS的模块,javascript,node.js,requirejs,node-modules,console.log,Javascript,Node.js,Requirejs,Node Modules,Console.log,我目前正在开发一个本地JavaScript应用程序,它将帮助我为特定客户动态生成一些管理文档,如未付服务的发票和凭证 我将使用node_模块,到目前为止我使用的是([“jsonfile”、“uniqid”、“mkdirp”)这些模块需要存储在变量中 问题是,我正在调用jsonfile,正如其文档中提到的那样 var jsonfile=require(“jsonfile”) 当console.log(jsonfile)从javascript文件返回function localRequire()时,
([“jsonfile”、“uniqid”、“mkdirp”)
这些模块需要存储在变量中
问题是,我正在调用jsonfile,正如其文档中提到的那样
var jsonfile=require(“jsonfile”)代码>
当console.log(jsonfile)
从javascript文件返回function localRequire()
时,与我在浏览器控制台中编写该命令不同。。为此,它会回来的
>> console.log(jsonfile)
Object { spaces: null, readFile: readFile(), readFileSync: readFileSync(), writeFile: writeFile(), writeFileSync: writeFileSync() }
undefined
为了使问题更加复杂,当从控制台或脚本调用readFile()
时,它返回的结果是readFile()
不是一个定义的函数,而是用于jsonfile或fs
的,后者是jsonfile
所需的节点模块
>>function readFile()
jsonfile.readFile()
TypeError: fs.readFile is not a function
我真的不知道与脚本变量有什么区别,以及如何从控制台使用该变量。尤其是在运行应用程序后,我不会在控制台中重新定义它
您可能需要的一些代码片段:
我正在调用index.html
中的两个脚本:
<!--requirejs -->
<script data-main="app" src="node_modules/requirejs/require.js"></script>
<!-- a script which will use $ which is not defined -->
<script src="services/specifyDocType.js"></script>
<!--the script the call require and have the jsonfile variable -->
<script src="jsonfile-test.js"></script>
main.js
:
define("jquery", function($) {
$('body');
});
对于将创建jsonfile变量的本地脚本,以下是我必须执行工作测试的初始代码:
var data = 'DOCS/voucher/voucher.json';
var jsonfile = require(["node_modules/jsonfile/index"]);
console.log(jsonfile);
//this console that returns the function localRequire() mentioned above.
jsonfile.readFile(data, function(err, obj) {
console.dir(obj);
});
对于这个应用程序,我不需要使用像Angularjs这样的框架,我需要为我目前使用的每个模块使用require在根目录中创建唯一的ID和目录
还有一件事,这是我第一次运行时的控制台:
function localRequire() jsonfile-test.js:4:1
TypeError: jsonfile.readFile is not a function[Learn More] jsonfile-test.js:6:1
ReferenceError: module is not defined[Learn More] index.js:133:1
Error: Module name "fs" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded require.js:168:17
Use of getPreventDefault() is deprecated. Use defaultPrevented instead.
您似乎认为RequireJS是节点的require
调用的替代品。事实并非如此
Node支持CommonJS模块系统。RequireJS支持异步模块定义(AMD)系统。(有关两者之间的区别的问题,请参阅。)它还提供了支持以方便消费CommonJS模块,但不支持按原样消费CommonJS模块。至少,CommonJS模块必须包装在define
调用中。例如,如果您从这个CommonJS模块开始:
function foo() {}
exports.foo = foo;
然后您至少需要将其转换为:
define(function (require, exports) {
function foo() {}
exports.foo = foo;
});
此外,尽管RequireJS可以使用CommonJS模块,但它无法为那些依赖于节点VM的模块提供支持fs
就是这样一个模块。它取决于节点VM。它不能在浏览器中运行。DOM不公开fs
模块可以用来访问文件系统的APIjsonfile
依赖于fs
,因此您不能在浏览器中用RequireJS加载它。您可能会找到一个在浏览器中运行的模块,该浏览器提供与fs
兼容的API,并向客户端代码提供一个伪文件系统,这可能会起作用,但您必须找到替换模块。RequireJS不会为您这样做。您解释得太多了,但我仍然不清楚您的问题是什么,您尝试了什么。真是一团糟,您想要构建一些东西,但您却以一种不可能工作的方式将后端代码与客户端代码混合在一起。Ijsonfile不是一个大问题,因此我需要使用uniqid和mkdirp!他们使用相同的nodedo要求,你认为我可以通过在服务器端运行这些代码来解决这个问题吗mkdirp
还取决于特定于节点的支持。是的,如果您将整个过程移动到服务器端,那么您可以使用Node运行整个过程,避免使用RequireJS,一切都会正常工作。
define(function (require, exports) {
function foo() {}
exports.foo = foo;
});