MWi*_*ead 2 javascript node-modules typescript
我正在自学更多关于如何创建节点模块以及如何使用打字稿的知识。为了实现这一点,我尝试在本地创建自己的节点模块,并在项目中引用它 - 但是,打字稿似乎无法识别我的模块。
我创建的节点模块是一个用于进行矩阵/向量运算的简单库,因为我会玩弄这些东西。我在项目的 node_modules 目录中创建了一个文件夹,其中包含以下文件:
node_modules
XVectorMath
package.json
vectorMath.js
vectorMath.d.ts
Run Code Online (Sandbox Code Playgroud)
这是编译这些文件的单独打字稿项目的输出。这些是相关文件的内容:
XVectorMath/package.json
{
"name": "XVectorMath",
"version": "0.1.0",
"main": "vectorMath.js",
"types": "vectorMath.d.ts"
}
Run Code Online (Sandbox Code Playgroud)
XVectorMath/vectorMath.d.ts
export declare class Vector3 {
constructor(x?: number, y?: number, z?: number);
x: number;
y: number;
z: number;
angle(): number;
magnitude(): number;
addVector3(b: Vector3): Vector3;
subtractVector(b: Vector3): Vector3;
divide(a: number): Vector3;
scale(a: number): Vector3;
normalize(): Vector3;
dotProduct(b: Vector3): number;
}
export declare class Matrix3 {
constructor(m?: any);
value: number[][];
getRowVector(n: number): Vector3;
getColumnVector(n: number): Vector3;
multiplyMatrix3(m: Matrix3): Matrix3;
addMatrix3(m: Matrix3): Matrix3;
}
Run Code Online (Sandbox Code Playgroud)
我没有包含 vectorMath.js 的内容,因为它有点冗长,似乎没有相关性。
在我的项目的其他地方,我有一个 main.ts 文件,其中包含以下导入语句:
src/main.ts
import {Vector3} from 'XVectorMath'
Run Code Online (Sandbox Code Playgroud)
在这里,我收到了一个打字稿错误:“找不到模块‘XVectorMath’”。
这是我的项目的 tsconfig.json:
配置文件
{
"compilerOptions":{
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
"include": [
"src/**/*.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
根据我所知道的,我认为这足以让 typescript 识别我的模块的名称(由主配置条目提供)并利用 vectorMath.d.ts 中提供的类型(由类型配置条目提供)。
我是否有任何这些配置不正确,或者我错过了任何步骤?或者我完全误解了什么?
编辑 我已经包含了运行 tsc --traceResolution 的结果:
c:\Develop\NodeScripts\Typescript Project Test>tsc --traceResolution
======== Resolving module 'XVectorMath' from 'c:/Develop/NodeScripts/Typescript
Project Test/src/main.ts'. ========
Module resolution kind is not specified, using 'NodeJs'.
Loading module 'XVectorMath' from 'node_modules' folder, target file type 'TypeS
cript'.
Directory 'c:/Develop/NodeScripts/Typescript Project Test/src/node_modules' does
not exist, skipping all lookups in it.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field 'vectorMath.d.ts' that references 'c:/Develop/N
odeScripts/Typescript Project Test/node_modules/XVectorMath/vectorMath.d.ts'.
Found 'package.json' at 'c:/Develop/NodeScripts/Typescript Project Test/node_mod
ules/XVectorMath/package.json'. Package ID is 'XVectorMath/vectorMath.d.ts@0.1.0
'.
File 'c:/Develop/NodeScripts/Typescript Project Test/node_modules/XVectorMath.ts
' does not exist.
File 'c:/Develop/NodeScripts/Typescript Project Test/node_modules/XVectorMath.ts
x' does not exist.
File 'c:/Develop/NodeScripts/Typescript Project Test/node_modules/XVectorMath.d.
ts' does not exist.
'package.json' does not have a 'typings' field.
'package.json' has 'types' field 'vectorMath.d.ts' that references 'c:/Develop/N
odeScripts/Typescript Project Test/node_modules/XVectorMath/vectorMath.d.ts'.
File 'c:/Develop/NodeScripts/Typescript Project Test/node_modules/XVectorMath/ve
ctorMath.d.ts' exist - use it as a name resolution result.
Resolving real path for 'c:/Develop/NodeScripts/Typescript Project Test/node_mod
ules/XVectorMath/vectorMath.d.ts', result 'c:/Develop/Libraries/Javascript/@type
s-XVectorMath/dist/vectorMath.d.ts'.
======== Module name 'XVectorMath' was successfully resolved to 'c:/Develop/Libr
aries/Javascript/@types-XVectorMath/dist/vectorMath.d.ts'. ========
Run Code Online (Sandbox Code Playgroud)
我不是 100% 确定这是在说 - 但我认为这意味着它找到了我的数学库项目的原始位置,在该位置找到了 .d.ts 文件,并使用它来解析模块名称.
这似乎不太正确 - 我将库输出文件安装到节点模块(使用 npm install 完成)的原因是将模块放在这个项目的 node_modules 文件夹中。
编辑 2
为了添加更多信息,我在下面包含了我的项目的 main.ts 文件以显示我的导入以及我的 package.json:
主文件
import {Vector3} from 'XVectorMath' // Cannot find module error
var test: string = "Main test";
let vec:Vector3 = new Vector3();
vec.x = 5; // No intellisense in VS Code, 'x' property considered an 'any' type
// instead of the number that it should be
Run Code Online (Sandbox Code Playgroud)
包.json
{
"name": "Test",
"version": "0.1.0",
"dependencies": {
"XVectorMath": "file:../../Libraries/Javascript/@types-XVectorMath/dist"
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*cky 16
我也遇到了这个问题,因此对于未来的流浪者,如果您的新 NodeJS TypeScript 应用程序存根无法识别和声 (ES6) 导入,只需将 "compilerOptions.moduleResolution":"node" 添加到您的项目 tsconfig.json 中:
这是我对 TensorFlow.js 的设置:
{
"compilerOptions": {
"skipLibCheck": true,
"target": "ES2018",
"module": "es2015",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true,
"alwaysStrict": true
"moduleResolution": "node"
}
}
Run Code Online (Sandbox Code Playgroud)
手册后引用:
有两种可能的模块解析策略:Node 和 Classic。您可以使用 --moduleResolution 标志来指定模块解析策略。如果未指定,默认为 Classic for --module AMD | 系统 | ES2015 或 Node 否则。
TL; 博士; “经典”策略不搜索 node_modules 目录!
| 归档时间: |
|
| 查看次数: |
6248 次 |
| 最近记录: |