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!
See Question&Answers more detail:
os