最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript cannot find module in node_modules that I created myself locally - Stack Overflow

programmeradmin2浏览0评论

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/[email protected]
'.
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"
  }
}

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/[email protected]
'.
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"
  }
}
Share Improve this question edited Aug 23, 2018 at 10:31 MWinstead asked Aug 22, 2018 at 17:02 MWinsteadMWinstead 1,3056 gold badges12 silver badges28 bronze badges 4
  • Looks like you have an extra colon in your package.json: "main:": "vectorMath.js" should be "main": "vectorMath.js". See if that fixes it. Otherwise look at the output of tsc with the --traceResolution flag for more hints. – Matt McCutchen Commented Aug 22, 2018 at 17:08
  • Sorry - it looks like that was an error that I added in when copying everything over to stackoverflow. I've updated the file to show the correct contents. I'll check out --traceResolution – MWinstead Commented Aug 22, 2018 at 17:18
  • The trace give you a hint that node_modules directory cannot be found. Directory 'c:/Develop/NodeScripts/Typescript Project Test/src/node_modules' does not exist, skipping all lookups in it. Could it be that your node_modules is either unreadable or in a different location than tsc is expecting it to be (e.g. not the root of the project) – VeldMuijz Commented Aug 23, 2018 at 11:43
  • That's correct - but I believe it later finds the directory at the project root, /Typescript Project Test/node_modules. The resolution strategy - as I understand it - starts from the directory of the file with the import statement, and if no matching module can be found, starts walking up the directory structure. Which is what happened here - nothing found in /src, so we step up to /Typescript Project Test and sure enough find a node_modules directory. – MWinstead Commented Aug 23, 2018 at 11:49
Add a comment  | 

1 Answer 1

Reset to default 21

I also hit this issue, so for future wanderers, if your new NodeJS TypeScript application stub does not recognize harmony (ES6) imports just add "compilerOptions.moduleResolution":"node" to yours project tsconfig.json:

Here is my setup for TensorFlow.js:

{
  "compilerOptions": {
    "skipLibCheck": true,
    "target": "ES2018",
    "module": "es2015",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "noImplicitAny": true,
    "alwaysStrict": true
    "moduleResolution": "node"
  }
}

Citation after manual:

There are two possible module resolution strategies: Node and Classic. You can use the --moduleResolution flag to specify the module resolution strategy. If not specified, the default is Classic for --module AMD | System | ES2015 or Node otherwise.

TL;DR; "classic" strategy does not search node_modules directory!

发布评论

评论列表(0)

  1. 暂无评论