简体   繁体   English

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

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

I am using typescript with node . 我正在将typescriptnode I can write this without any problems: 我可以写这个没有任何问题:

import * as $ from "jquery";

The definition file for jquery is in node_modules/@types/jquery . jquery的定义文件在node_modules/@types/jquery However, neither of the following works with the definition file for decimal.js being in node_modules/decimal.js : 然而,无论是与定义文件的以下作品decimal.js在被node_modules/decimal.js

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

If I however include the file with its absolute path it works like a charm: 但是,如果我将文件包含在其绝对路径中,则它的作用就像一个超级按钮:

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

I'm using the latest version available in npm and these command line parameters: 我正在使用npm可用的最新版本以及以下命令行参数:

--removeComments --inlineSourceMap --inlineSources --allowSyntheticDefaultImports --charset UTF-8 --module amd --target ES6 --newLine LF --moduleResolution classic --removeComments --inlineSourceMap --inlineSources --allowSyntheticDefaultImports --charset UTF-8-模块amd-目标ES6 --newLine LF --moduleResolution经典

Considering that your module is in node_modules , you want to use the node style of module resolution. 考虑到您的模块位于node_modules ,您想使用模块解析的node样式。 Replace --moduleResolution classic with --moduleResolution Node . --moduleResolution Node替换--moduleResolution classic

See here 这里

However, resolution for a non-relative module name is performed differently. 但是,非相对模块名称的解析是不同的。 Node will look for your modules in special folders named node_modules. Node将在名为node_modules的特殊文件夹中查找您的模块。 A node_modules folder can be on the same level as the current file, or higher up in the directory chain. node_modules文件夹可以与当前文件位于同一级别,也可以位于目录链中的更高级别。 Node will walk up the directory chain, looking through each node_modules until it finds the module you tried to load. 节点将遍历目录链,遍历每个node_modules,直到找到您尝试加载的模块。

The classic style resolves modules differently. 经典样式以不同的方式解析模块。 According to the linked source: 根据链接的来源:

This used to be TypeScript's default resolution strategy. 这曾经是TypeScript的默认解析策略。 Nowadays, this strategy is mainly present for backward compatibility. 如今,这种策略主要用于向后兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM