Oclif v3 - Module Load Error
11/05/2023, Sun
Categories:
#shell
#typescript
Tags:
#NodeJs
Typescript Import Aliases
To simplify the referencing of module import paths, one can configure the ts.config.json
like so.
{
"compilerOptions": {
...
"paths": {
"@src/*": [
"src/*"
]
},
"rootDir": "src",
"baseUrl": ".",
},
"include": [
"src/**/*"
]
}
To be able to use this import
import { typeCheck, stringTypes } from '@src/utilities/type-check';
over something like this
import { typeCheck, stringTypes } from '../utilities/type-check';
Using aliases makes for more intuitive file path referencing if you decide to rearrange certain files.
However, when using TypeScript with Oclif, the TypeScript custom import aliases might produce this error when executing
[MODULE_NOT_FOUND] ModuleLoadError Plugin:
To remedy this problem, install tsconfig-paths and add the below changes into the "bin/dev" file and execute the "bin/dev" file as you normally would.
Configure the "bin/dev" file.
#!/usr/bin/env node
const oclif = require('@oclif/core')
const path = require('path')
const tsConfigPaths = require('tsconfig-paths');
const project = path.join(__dirname, '..', 'tsconfig.json')
// In dev mode -> use ts-node and dev plugins
process.env.NODE_ENV = 'development'
require('ts-node').register({project})
tsConfigPaths.register();
// In dev mode, always show stack traces
oclif.settings.debug = true;
// Start the CLI
oclif.run().then(oclif.flush).catch(oclif.Errors.handle)
Then execute the "dev" file to run the cli.
./bin/dev <your-command>