Electron + Angular 2&中的Node.js模块查找TypeScript应用程序 [英] Node.js module lookup in Electron+Angular 2 & TypeScript application
问题描述
我正在使用Angular 2开发电子应用程序。
我遵循了此教程最初用于设置环境。
我的设置有点复杂,但总的来说非常相似。
tsconfig .json
:
{
compilerOptions:{
target : es5,
module: commonjs,
moduleResolution: node,
sourceMap:true,
emitDecoratorMetadata:true,
experimentalDecorators:true,
removeComments:false,
noImplicitAny:false
},
exclude:[
node_modules ,
app / node_modules,
dist
]
}
systemjs.config.js
就像本教程一样,以及 index.html
我的一个模块(位于 app
文件夹中- app / * / * / * / module
)取决于 node-ffi
。所以我已经在 typings.json
中添加了必要的类型:
{
globalDependencies:{
core-js:注册表:dt / core-js,
茉莉花:注册表:dt /茉莉花,
node: registry:dt / node,
ref: registry:dt / ref,
ref-struct: registry:dt / ref-struct,
ffi:注册表:dt / node-ffi
}
}
现在,该模块尝试使用 ffi
这样:
从'ffi'导入{DynamicLibrary,Library,types};
出口类别CppProxy {
//某些代码
}
最终被转换为:
var ffi_1 = require('ffi');
//使用ffi_1导出的东西
根据这篇文章中,有一个定义明确的 node.js
模块查找的方法,根据这种方式,它应该找到 node-ffi
模块为 node-ffi
驻留在 app / node_modules
中。但是,事实并非如此。它只在 app
文件夹中查找 ffi.js
文件夹,显然找不到它。
我第一次尝试修复它是在 systemjs.config.js
的地图部分添加 ffi
条目,如下所示:
var map = {
'app':'。',//'dist',
'@angular':'node_modules / @ angular',
'angular2-in-memory-web-api':'node_modules / angular2-in-memory-web-api',
'rxjs': 'node_modules / rxjs',
'node-binary':'node_modules / systemjs-plugin-node-binary / node-binary.js',
'ffi':'node_modules / ffi / lib / ffi .js'
};
它有助于加载 ffi
,但带来了出现新问题。 ffi
本身取决于其他模块:
var ref = require(' ref')
var assert = require('assert')
var debug = require('debug')('ffi:ffi')
var Struct = require('ref-struct' )
var绑定= require('./ bindings')
现在应用程序失败了查找这些模块。我也尝试将其中一些添加到地图中,但是同样,它只是解决了一级依赖关系。
在我看来,要求
不应该只在 app
文件夹,我不想递归地将所有依赖项添加到 systemjs.config.js
的map部分。
我在做什么错?
更新:
还有另一个问题处理了一个非常类似的问题,但它专门询问有关使用 require('remote')
。
我是问我如何使用 Node.js
模块解析机制,同时仍然使用 System.js
作为模块加载器。
为 Pace 在他的评论之一中提到, System.js
覆盖了 Node.js
的要求
方法并使用它自己的解析机制。这就是为什么 require
方法不会遵循 Node.js
查找机制。但是,有一种方法可以使用后者:
System.js
存储 Node .js
的 require
在 _nodeRequire
变量中。因此,使用 Node.js
机制加载模块的方法是
var模块= System._nodeRequire('./ path / to / module /')
此处是有助于我提出此解决方案的讨论。 / p>
I'm developing an Electron application with Angular 2.
I followed this tutorial to initially setup the environment.
My setup is a bit more complicated but in general is very similar.
tsconfig.json
:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules",
"app/node_modules",
"dist"
]
}
systemjs.config.js
is just like in the tutorial as well as index.html
One of my modules (which resides somewhere inside the app
folder - app/*/*/*/module
) depends on node-ffi
. So I've added the necessary typings to typings.json
:
{
"globalDependencies": {
"core-js": "registry:dt/core-js",
"jasmine": "registry:dt/jasmine",
"node": "registry:dt/node",
"ref": "registry:dt/ref",
"ref-struct": "registry:dt/ref-struct",
"ffi": "registry:dt/node-ffi"
}
}
Now, the module tries to use ffi
like this:
import { DynamicLibrary, Library, types } from 'ffi';
export class CppProxy {
//Some code
}
Which is eventually transpiled to:
var ffi_1 = require('ffi');
//Utilize ffi_1 exported stuff
According to this article, there is a well-defined way for node.js
module lookup and according to this way it should find the node-ffi
module as node-ffi
resides in app/node_modules
. However, it doesn't. It only looks in app
folder for ffi.js
and obviously fails to find it.
My first attempt to fix it was adding ffi
entry to the map section of systemjs.config.js
, like this:
var map = {
'app': '.', // 'dist',
'@angular': 'node_modules/@angular',
'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
'rxjs': 'node_modules/rxjs',
'node-binary': 'node_modules/systemjs-plugin-node-binary/node-binary.js',
'ffi': 'node_modules/ffi/lib/ffi.js'
};
It helped to load ffi
, but brought up new problems. ffi
itself depends on other modules:
var ref = require('ref')
var assert = require('assert')
var debug = require('debug')('ffi:ffi')
var Struct = require('ref-struct')
var bindings = require('./bindings')
So now the application failed to find these modules. I've tried to add some of them to the map too, but again it just solved one level of dependencies.
It doesn't seem right to me, require
shouldn't be looking only in app
folder and I don't feel like adding all the dependencies recursively to map section of systemjs.config.js
.
What am I doing wrong?
Upd:
There another question dealing with a pretty similar issue, but it is asking specifically about using require('remote')
.
I'm asking how can I use a Node.js
module resolution mechanism while still using System.js
as a module loader.
As Pace mentioned in one of his comments, System.js
overrides Node.js
's require
method and uses it's own resolution mechanism. This is why the require
method won't follow the Node.js
lookup mechanism. However there is a way to use the latter:
System.js
stores the Node.js
's require
in _nodeRequire
variable. So the way to load a module using Node.js
mechanism is to load it by
var module = System._nodeRequire('./path/to/module/')
Here is the discussion that helped me to come up with this solution.
这篇关于Electron + Angular 2&中的Node.js模块查找TypeScript应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!