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

TypeScript cannot find module decimal.js - but absolute path works

我正在使用 typescriptnode。我可以毫无问题地写这个:

import * as $ from "jquery";

jquery 的定义文件在 node_modules/@types/jquery 中。但是,以下均不适用于 decimal.jsnode_modules/decimal.js 中的定义文件:

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

但是,如果我包含带有绝对路径的文件,它就会像一个魅力:

import { Decimal } from "/path/to/project/node_modules/decimal.js/decimal";

我正在使用 npm 中可用的最新版本和这些命令行参数:

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

考虑到您的模块在 node_modules 中,您想使用 node 样式的模块解析。将 --moduleResolution classic 替换为 --moduleResolution Node.

here

However, resolution for a non-relative module name is performed differently. Node will look for your modules in special folders named node_modules. A node_modules folder can be on the same level as the current file, or higher up in the directory chain. Node will walk up the directory chain, looking through each node_modules until it finds the module you tried to load.

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

This used to be TypeScript’s default resolution strategy. Nowadays, this strategy is mainly present for backward compatibility.