1

I am using typescript with node. I can write this without any problems:

import * as $ from "jquery";

The definition file for jquery is in node_modules/@types/jquery. However, neither of the following works with the definition file for decimal.js being in 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:

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

msrd0
  • 7,816
  • 9
  • 47
  • 82

1 Answers1

0

Considering that your module is in node_modules, you want to use the node style of module resolution. Replace --moduleResolution classic with --moduleResolution Node.

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. 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.

The classic style resolves modules differently. According to the linked source:

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

pushkin
  • 9,575
  • 15
  • 51
  • 95
  • Do you know if there is any way to tell the typescript compiler to output `decimal` instead of `decimal.js`? It seems that `require.js` treats the first relative to the script path and the later relative to the html path – msrd0 Jan 10 '18 at 06:09
  • I'm not sure what this means: "It seems that require.js treats the first relative to the script path and the later relative to the html path." The compiler is supposed to compile to javascript, so I'm not sure why you don't want the file to be of that type. You might want to look into the `--outFile` flag. – pushkin Jan 10 '18 at 16:03
  • Oh I want typescript to output `define(...decimal...)` instead of `define(...decimal.js...)` because for the first require.js loads /static/decimal.js, for the second it tries ./decimal.js – msrd0 Jan 10 '18 at 18:30
  • @msrd0 [Here](https://www.typescriptlang.org/docs/handbook/compiler-options.html) is the list of compiler options. I didn't see an option for what you wanted, and I wouldn't expect there to be one like that. Frankly, I don't really understand the issue. You're saying that by default, when you import from `decimal`, it still can't find the right one, because it's looking in the wrong place? Or does this happen only when you use `require`? – pushkin Jan 11 '18 at 05:07
  • I am saying that `require.js` treats dependencies with a `.js` ending differently than those without, but I assume it's a problem with `require.js` and not with typescript - just curious if typescript can provide a workaround since `require.js` seems to ignore the configuration option – msrd0 Jan 11 '18 at 05:09
  • I am not aware of that. What exactly does your `require` statement look like? From what path are you requiring? What absolute path is it looking for? What absolute path are you expecting? Alternatively, go [here](https://www.typescriptlang.org/docs/handbook/module-resolution.html) and search for "require". That describes the rules of module resolution in typescript. I imagine you'll find the answer there. – pushkin Jan 11 '18 at 05:14
  • I think you didn't really understand - I am using a javascript library called `require.js` (to support module resolution in web browsers) that is supposed to load all javascript files from one location but fails to do so – msrd0 Jan 11 '18 at 05:16