I'm teaching myself more about how to create node modules and how to use typescript. To accomplish this, I've attempted to create my own node module locally, and reference it in a project - however, typescript doesn't seem to recognize my module.
The node module I've created is a simple library for doing matrix / vector operations, since I'll be toying around with such things. I've created a folder inside my project's node_modules directory, which has the following files:
node_modules
XVectorMath
package.json
vectorMath.js
vectorMath.d.ts
This is the output of a separate typescript project which compiled these files. These are the contents of the relevant files:
XVectorMath/package.json
{
"name": "XVectorMath",
"version": "0.1.0",
"main": "vectorMath.js",
"types": "vectorMath.d.ts"
}
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;
}
I haven't included the contents of vectorMath.js, because it's a little lengthy and doesn't seem to be relevant.
Elsewhere in my project I have a main.ts file, with the following import statement:
src/main.ts
import {Vector3} from 'XVectorMath'
It's here that I get a typescript error: "Cannot find modules 'XVectorMath'".
Here is the tsconfig.json of my project:
tsconfig.json
{
"compilerOptions":{
"module": "commonjs",
"outDir": "dist",
"sourceMap": true
},
"include": [
"src/**/*.ts"
]
}
Based on what I know, I would think this would be enough for typescript to recognize the name of my module (provided by the main config entry) and utilize the types provided in vectorMath.d.ts (provided by the types config entry).
Do I have any of this configured incorrectly, or have I missed any steps? Or do I misunderstand something entirely?
EDIT I've included the results from running 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'. ========
I'm not 100% sure this is saying - but I think it means that it has found the original location of my math library project, has found the .d.ts file in that location, and is using that to resolve the module name.
Which doesn't seem quite right - the reason I installed the library output files into node-modules (done using npm install ) was to have the modules in the node_modules folder of THIS project.
EDIT 2
To add more information, I've included my project's main.ts file below to show my import, as well as my package.json:
main.ts
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": "Test",
"version": "0.1.0",
"dependencies": {
"XVectorMath": "file:../../Libraries/Javascript/@types-XVectorMath/dist"
}
}