【问题标题】:Typescript cannot find module in node_modules that I created myself locallyTypescript 在我自己本地创建的 node_modules 中找不到模块
【发布时间】:2019-01-29 00:13:58
【问题描述】:

我正在自学更多关于如何创建节点模块以及如何使用打字稿的知识。为此,我尝试在本地创建自己的节点模块,并在项目中引用它 - 但是,打字稿似乎无法识别我的模块。

我创建的节点模块是一个用于执行矩阵/向量运算的简单库,因为我会玩弄这些东西。我在项目的 node_modules 目录中创建了一个文件夹,其中包含以下文件:

node_modules
  XVectorMath
    package.json
    vectorMath.js
    vectorMath.d.ts

这是编译这些文件的单独打字稿项目的输出。这些是相关文件的内容:

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;
}

我没有包含 vectorMath.js 的内容,因为它有点冗长,而且似乎不相关。

在我的项目的其他地方,我有一个 main.ts 文件,其中包含以下导入语句:

src/main.ts

import {Vector3} from 'XVectorMath'

在这里我收到一个打字稿错误:“找不到模块'XVectorMath'”。

这是我项目的 tsconfig.json:

tsconfig.json

{
    "compilerOptions":{
        "module": "commonjs",
        "outDir": "dist",
        "sourceMap": true
    },
    "include": [
        "src/**/*.ts"
    ]
}

根据我所知道的,我认为这足以让 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'. ========

我不是 100% 肯定这是在说 - 但我认为这意味着它找到了我的数学库项目的原始位置,在该位置找到了 .d.ts 文件,并正在使用它来解决模块名称。

这似乎不太正确 - 我将库输出文件安装到 node-modules 中(使用 npm install 完成)的原因是将模块放在这个项目的 node_modules 文件夹中。

编辑 2

为了添加更多信息,我在下面包含了我的项目的 main.ts 文件以显示我的导入,以及我的 package.json:

ma​​in.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"
  }
}

【问题讨论】:

  • 看起来您的package.json 中有一个额外的冒号:"main:": "vectorMath.js" 应该是"main": "vectorMath.js"。看看能不能解决。否则,请查看带有--traceResolution 标志的tsc 的输出以获取更多提示。
  • 抱歉 - 这是我在将所有内容复制到 stackoverflow 时添加的错误。我已更新文件以显示正确的内容。我会检查 --traceResolution
  • 跟踪提示您找不到 node_modules 目录。 Directory 'c:/Develop/NodeScripts/Typescript Project Test/src/node_modules' does not exist, skipping all lookups in it. 可能是您的 node_modules 不可读或位于与tsc 预期不同的位置(例如,不是项目的根目录)
  • 没错——但我相信它稍后会在项目根目录下找到目录,/Typescript Project Test/node_modules。解析策略 - 据我了解 - 从带有 import 语句的文件目录开始,如果找不到匹配的模块,则开始遍历目录结构。这就是这里发生的事情 - 在 /src 中找不到任何东西,所以我们升级到 /Typescript Project Test 并确实找到了一个 node_modules 目录。

标签: javascript typescript node-modules


【解决方案1】:

我也遇到了这个问题,所以对于未来的流浪者,如果您的新 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"
  }
}

manual之后的引用:

有两种可能的模块解析策略:Node 和 Classic。您可以使用 --moduleResolution 标志来指定模块解析策略。如果未指定,则默认为 Classic for --module AMD |系统 | ES2015 或 Node 其他。

TL;DR; “经典”策略不搜索 node_modules 目录!

【讨论】:

  • 就我而言,我需要"moduleResolution": "node""allowJs": true。很棒的提示!
猜你喜欢
  • 2019-07-08
  • 2015-06-30
  • 1970-01-01
  • 2019-05-11
  • 2016-06-18
  • 2019-10-18
  • 1970-01-01
  • 2013-04-03
  • 2017-06-29
相关资源
最近更新 更多