Node.js Electron+Angular 2 和 TypeScript 应用程序中的模块查找
Node.js module lookup in Electron+Angular 2 & TypeScript application
我正在使用 Angular 2.
开发 Electron 应用程序
我按照 this 教程初步设置了环境。
我的设置有点复杂,但大体上非常相似。
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": "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"
}
}
现在,模块尝试像这样使用 ffi
:
import { DynamicLibrary, Library, types } from 'ffi';
export class CppProxy {
//Some code
}
最终被编译为:
var ffi_1 = require('ffi');
//Utilize ffi_1 exported stuff
根据 this 文章,有一个定义明确的 node.js
模块查找方式,根据这种方式,它应该找到 node-ffi
模块作为 node-ffi
居住在 app/node_modules
。然而,事实并非如此。它只在 app
文件夹中查找 ffi.js
,但显然找不到它。
我第一次尝试修复它是将 ffi
条目添加到 systemjs.config.js
的地图部分,如下所示:
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 bindings = require('./bindings')
所以现在应用程序找不到这些模块。我也尝试将其中一些添加到地图中,但它又一次只解决了一级依赖关系。
这对我来说似乎不对,require
不应该只在 app
文件夹中查找,我不想将所有依赖项递归添加到 [= 的映射部分19=]。
我究竟做错了什么?
更新:
还有另一个 处理一个非常相似的问题,但它特别询问有关使用 require('remote')
.
我在问如何在仍然使用 System.js
作为模块加载器的同时使用 Node.js
模块解析机制。
作为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。但是有一种方法可以使用后者:
System.js
将 Node.js
的 require
存储在 _nodeRequire
变量中。所以使用Node.js
机制加载模块的方法是通过
加载它
var module = System._nodeRequire('./path/to/module/')
Here 是帮助我想出这个解决方案的讨论。
我正在使用 Angular 2.
开发 Electron 应用程序
我按照 this 教程初步设置了环境。
我的设置有点复杂,但大体上非常相似。
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": "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"
}
}
现在,模块尝试像这样使用 ffi
:
import { DynamicLibrary, Library, types } from 'ffi';
export class CppProxy {
//Some code
}
最终被编译为:
var ffi_1 = require('ffi');
//Utilize ffi_1 exported stuff
根据 this 文章,有一个定义明确的 node.js
模块查找方式,根据这种方式,它应该找到 node-ffi
模块作为 node-ffi
居住在 app/node_modules
。然而,事实并非如此。它只在 app
文件夹中查找 ffi.js
,但显然找不到它。
我第一次尝试修复它是将 ffi
条目添加到 systemjs.config.js
的地图部分,如下所示:
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 bindings = require('./bindings')
所以现在应用程序找不到这些模块。我也尝试将其中一些添加到地图中,但它又一次只解决了一级依赖关系。
这对我来说似乎不对,require
不应该只在 app
文件夹中查找,我不想将所有依赖项递归添加到 [= 的映射部分19=]。
我究竟做错了什么?
更新:
还有另一个 require('remote')
.
我在问如何在仍然使用 System.js
作为模块加载器的同时使用 Node.js
模块解析机制。
作为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。但是有一种方法可以使用后者:
System.js
将 Node.js
的 require
存储在 _nodeRequire
变量中。所以使用Node.js
机制加载模块的方法是通过
var module = System._nodeRequire('./path/to/module/')
Here 是帮助我想出这个解决方案的讨论。