TypeScript找不到模块decimal.js - 但绝对路径有效

时间:2018-01-10 05:36:31

标签: node.js typescript npm import decimal.js

我正在使用typescript node。我可以毫无问题地写出来:

import * as $ from "jquery";

jquery的定义文件位于node_modules/@types/jquery。但是,以下任何一项都不适用decimal.js的{​​{1}}定义文件:

node_modules/decimal.js

但是,如果我将文件包含在其绝对路径中,则它就像魅力一样:

import { Decimal } from "decimal";
import { Decimal } from "decimal.js";

我使用import { Decimal } from "/path/to/project/node_modules/decimal.js/decimal"; 中提供的最新版本以及这些命令行参数:

  

- removeComments --inlineSourceMap --inlineSources --allowSyntheticDefaultImports --charset UTF-8 --module amd --target ES6 --newLine LF --moduleResolution classic

1 个答案:

答案 0 :(得分:0)

考虑到您的模块位于node_modules,您希望使用node样式的模块解析。将--moduleResolution classic替换为--moduleResolution Node

请参阅here

  

但是,非相对模块名称的解析以不同方式执行。 Node将在名为node_modules的特殊文件夹中查找您的模块。 node_modules文件夹可以与当前文件位于同一级别,也可以位于目录链中的较高位置。 Node将走向目录链,查看每个node_modules,直到找到您尝试加载的模块。

经典风格以不同方式解析模块。根据链接来源:

  

这曾经是TypeScript的默认解析策略。如今,这种策略主要是为了向后兼容。

相关问题