require('os')中的平台问题 [英] Platform issue in require('os')

查看:640
本文介绍了require('os')中的平台问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Ionic 3(A5)应用程序.我在Mac上将其作为node-webkit(NW.JS)应用程序运行.如果我在index.html脚本标记中编写并检查,require('os')平台将返回' darwin ',而require('fs')会正确返回完整的对象集.但是,如果我在.ts文件中编写相同的脚本-require('os')平台返回'浏览器',而require('fs')返回空对象.

I have created an Ionic 3(A5) app. I am running this as node-webkit (NW.JS) app on Mac. If I write inside index.html script tag and check, require('os') platform returns 'darwin' and require('fs') returns full set of object correctly. But if I write same script inside a .ts file - require('os') platform returns 'browser' and require('fs') returns empty object.

我在devDependencies中使用@ types/node.

I am using @types/node in devDependencies.

index.html中的代码-

Code inside index.html -

 var os = require('os');
 var fs = require('fs');
 console.log('Log from index.html');
 console.log('platform = ' + os.platform());
 console.log('fs = ');
 console.log(fs);

app.component.ts中的代码-

Code inside app.component.ts -

var os = require('os');
var fs = require('fs');
console.log('Log from app.component.ts');
console.log('platform = ' + os.platform());
console.log('fs = ');
console.log(fs);

推荐答案

基于The Jared Wilcurt @TheJaredWilcurt在上的回复gitter.im

Based on a reply from The Jared Wilcurt @TheJaredWilcurt on gitter.im,

节点和Chromium不知道什么是TS文件.您需要使用一些东西来将其转换为可以实际运行的代码. TS是一种元语言,例如Markdown,Sass,HAML,CoffeeScript,JSX等.Node仅了解JavaScript,Chromium仅了解HTML,CSS和JS. 如果您正在使用某种东西来转换它,并且它改变了您的require语句,那么就是问题所在.您应该检查要告诉环境运行的实际代码.

Node and Chromium don't know what a TS file is. you need to use something to transpile it to code that can actually be ran. TS is a meta-language, like Markdown, Sass, HAML, CoffeeScript, JSX, etc. Node only understands JavaScript, Chromium only understands HTML, CSS, and JS. If you are using something to transpile it and it alters your require statements, then that is the problem. You should inspect the actual code you are telling the environment to run.

之所以发生这种情况,是因为Ionic编译器正在以某种方式更改代码.我更改了代码

This was happening because the Ionic transpiler was changing the code in some way. I changed the code

来自

var os = require('os');
var fs = require('fs');

var os = nw.require('os');
var fs = nw.require('fs');

现在唯一的问题是TypeScript编译器无法识别" nw ",因为它应该在运行时出现.我添加了

Now the only problem was that TypeScript compiler was not recognizing 'nw' as it was supposed to come at runtime. I have added

declare var nw: any;

在顶部. 一切都很好.

on top. All fine now.

这篇关于require('os')中的平台问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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