Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find Typescript module even though tsc successfully manages to resolve it

I have a Node.js project written in Typescript which is expected to run as a CLI, and am having trouble to import a module located out of the node_modules directory using an absolute path (relative paths work fine). It might be important to mention that I am using the oclif framework for building my CLI.

My project is organized as follows:

cli
 |--node_modules
 |--src
     |--my-module.ts
     |--subdir
          |--index.ts

Within my-module.ts I have:

 export class MyClass {
     myClassFcn(s: string) {
         return 'result'
     }
 }

The index.ts script contains something like:

 import {MyClass} = require('my-module')

When I try to execute my app with ts-node, I get

(node:10423) [MODULE_NOT_FOUND] Error Plugin: cli: Cannot find module 'my-module'
    module: @oclif/[email protected]
    task: toCached
    plugin: cli
    root: /home/eschmidt/Workspace/cli
    Error Plugin: cli: Cannot find module 'my-module'
        at Function.Module._resolveFilename (internal/modules/cjs/loader.js:571:15)
        at Function.Module._load (internal/modules/cjs/loader.js:497:25)
        at Module.require (internal/modules/cjs/loader.js:626:17)
        at require (internal/modules/cjs/helpers.js:20:18)
        at Object.<anonymous> (/home/eschmidt/Workspace/cli/src/commands/create/index.ts:5:1)
        at Module._compile (internal/modules/cjs/loader.js:678:30)
        at Module.m._compile (/home/eschmidt/Workspace/cli/node_modules/ts-node/src/index.ts:403:23)
        at Module._extensions..js (internal/modules/cjs/loader.js:689:10)
        at Object.require.extensions.(anonymous function) [as .ts] (/home/eschmidt/Workspace/cli/node_modules/ts-node/src/index.ts:406:12)
        at Module.load (internal/modules/cjs/loader.js:589:32)
    module: @oclif/[email protected]
    task: toCached
    plugin: my-plugin
    root: /home/eschmidt/Workspace/cli

What I can't understand is that when I run tsc --traceResolution the module is correctly resolved:

======== Module name 'my-module' was successfully resolved to '/home/eschmidt/Workspace/cli/src/my-module.ts'. ========

My tsconfig.json file contains:

{
  "compilerOptions": {
    "declaration": true,
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "importHelpers": true,
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./lib",
    "pretty": true,
    "rootDirs": [
      "./src/"
    ],
    "strict": true,
    "target": "es2017",
    "baseUrl": "src"
  },
  "include": [
    "./src/**/*"
  ]
}

I would greatly appreciate it if anyone could shed some light on this issue, or at least suggest where to look for further help. In case more details are needed, please let me know.

Thanks in advance!

like image 871
edu_ Avatar asked May 29 '18 22:05

edu_


1 Answers

It turns out that the problem was due to the fact that although both tsc and ts-node use baseUrl for absolute path resolution, neither of them perform any type of actual mapping from absolute to relative paths in the generated Javascript code. In other words, both the transpiled JS files and the code produced internally by ts-node end up having:

import  {MyClass} = require('my-module')

whereas I was expecting them to contain something like:

import  {MyClass} = require('../my-module')

which prevented node's module loader from finding the module. ts-node also did not work, I believe, because there was simply no tsconfig.json file to indicate the path mappings.

Although confusing IMO, and not properly documented, this is expected behavior, though, as discussed here. As of now, absolute to relative path mapping is not supported by Typescript (see https://github.com/Microsoft/TypeScript/issues/15479).

In order to avoid the situation known as path hell, which means having very deep relative import paths, I found module-alias and tsmodule-alias to be very useful. These modules alter the behavior of the module loader so that it automatically maps aliases to relative paths.

For more information about the problem, refer to this issue on Github.

like image 100
edu_ Avatar answered Oct 05 '22 14:10

edu_