Javascript Typescript在我自己本地创建的节点_模块中找不到模块
我在自学如何创建节点模块以及如何使用typescript。为了实现这一点,我尝试在本地创建自己的节点模块,并在项目中引用它——然而,typescript似乎无法识别我的模块 我创建的节点模块是一个简单的库,用于执行矩阵/向量操作,因为我将玩弄这些东西。我在项目的node_modules目录中创建了一个文件夹,其中包含以下文件:Javascript Typescript在我自己本地创建的节点_模块中找不到模块,javascript,typescript,node-modules,Javascript,Typescript,Node Modules,我在自学如何创建节点模块以及如何使用typescript。为了实现这一点,我尝试在本地创建自己的节点模块,并在项目中引用它——然而,typescript似乎无法识别我的模块 我创建的节点模块是一个简单的库,用于执行矩阵/向量操作,因为我将玩弄这些东西。我在项目的node_modules目录中创建了一个文件夹,其中包含以下文件: node_modules XVectorMath package.json vectorMath.js vectorMath.d.ts 这是
node_modules
XVectorMath
package.json
vectorMath.js
vectorMath.d.ts
这是编译这些文件的单独typescript项目的输出。以下是相关文件的内容:
XVectorMath/package.json
{
"name": "XVectorMath",
"version": "0.1.0",
"main": "vectorMath.js",
"types": "vectorMath.d.ts"
}
{
"compilerOptions":{
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
"include": [
"src/**/*.ts"
]
}
{
"name": "Test",
"version": "0.1.0",
"dependencies": {
"XVectorMath": "file:../../Libraries/Javascript/@types-XVectorMath/dist"
}
}
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;
}
import {Vector3} from 'XVectorMath'
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
我没有包括vectorMath.js的内容,因为它有点长,而且似乎不相关
在项目的其他地方,我有一个main.ts文件,其中包含以下导入语句:
src/main.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;
}
import {Vector3} from 'XVectorMath'
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
就是在这里,我得到了一个typescript错误:“找不到模块‘XVectorMath’”
以下是我的项目的tsconfig.json:
tsconfig.json
{
"name": "XVectorMath",
"version": "0.1.0",
"main": "vectorMath.js",
"types": "vectorMath.d.ts"
}
{
"compilerOptions":{
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
"include": [
"src/**/*.ts"
]
}
{
"name": "Test",
"version": "0.1.0",
"dependencies": {
"XVectorMath": "file:../../Libraries/Javascript/@types-XVectorMath/dist"
}
}
根据我所知,我认为这足以让typescript识别模块的名称(由主配置项提供)并利用vectorMath.d.ts中提供的类型(由types config项提供)
我是否有任何配置错误,或我是否错过了任何步骤?还是我完全误解了什么
编辑
我已经包括了运行tsc的结果--跟踪解决方案:
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'. ========
我不是100%确定这是什么意思,但我认为这意味着它已经找到了我的数学库项目的原始位置,在该位置找到了.d.ts文件,并正在使用该文件解析模块名
这似乎不太正确-我将库输出文件安装到节点模块(使用npm安装完成)的原因是将模块放在这个项目的node_modules文件夹中
编辑2
为了添加更多信息,我在下面包含了我的项目的main.ts文件,以显示我的导入以及我的package.json:
main.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;
}
import {Vector3} from 'XVectorMath'
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
package.json
{
"name": "XVectorMath",
"version": "0.1.0",
"main": "vectorMath.js",
"types": "vectorMath.d.ts"
}
{
"compilerOptions":{
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
"include": [
"src/**/*.ts"
]
}
{
"name": "Test",
"version": "0.1.0",
"dependencies": {
"XVectorMath": "file:../../Libraries/Javascript/@types-XVectorMath/dist"
}
}
我也提到了这个问题,因此对于未来的漫游者,如果您的新NodeJS TypeScript应用程序存根不识别harmony(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"
}
}
引文如下:
有两种可能的模块解析策略:Node和Classic。您可以使用--moduleResolution标志指定模块解析策略。如果未指定,则默认值为--module AMD | System | ES2015的Classic,否则为Node
TL;博士“经典”策略不搜索节点\模块目录看起来您的
包中有一个额外的冒号。json
:“main::“vectorMath.js”
应该是“main:”vectorMath.js“
。看看这能不能解决问题。否则,请使用--traceResolution
标志查看tsc
的输出,以获取更多提示。抱歉,这似乎是我在将所有内容复制到stackoverflow时添加的错误。我已更新该文件以显示正确的内容。我将签出--traceresolution跟踪会提示您找不到node_modules目录<代码>目录“c:/develope/NodeScripts/Typescript Project Test/src/node_modules”不存在,将跳过其中的所有查找。可能是您的node_模块不可读,或者与tsc
预期的位置不同(例如,不是项目的根目录)这是正确的-但我相信它稍后会在项目根目录/Typescript project Test/node_modules中找到目录。解析策略——据我所知——从带有import语句的文件目录开始,如果找不到匹配的模块,则开始向目录结构上移动。这就是这里发生的事情-在/src中找不到任何东西,所以我们升级到/Typescript项目测试,并确保找到一个node_modules目录。在我的例子中,我需要“moduleResolution”:“node”
以及“allowJs”:true
。好提示!