node.jstypescriptwebstormnestjsts-node

Nestjs: import modules undefined, but methods and functions from modules can be imported


I am using Nestjs with WebStorm & TS 4.2.3^latest.

The problem that I am facing is a bit strange. For example, some modules, like axios can be installed, imported, and used as usual. But some modules, especially Nodejs Core, like fs or path, can't be imported as modules. BUT their methods can be imported and used just fine!

// ERROR: Module undefined on run:dev, but no error in IDE
import path from 'path';
import fs from 'fs';

// Working fine
import { join } from 'path';
import { readFileSync } from 'path';

I am sure, they have correct TS types, even installed manually. For example:

import axios from 'axios';
import path from 'path'; // path is undefined
import { join } from 'path';
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  async test(input: string): Promise<void> {
    // working fine
    await axios.get() 
    // Cannot read property 'join' of undefined
    await path.join() 

    // await join() Works fine!
  }
}

I have only one tsconfig.json which is generated by Nest Cli. I am starting my apps via npm start:dev -name and IDE doesn't show any errors in code until I run code.

Example

tsconfig.json module part, just to be sure: "module": "commonjs", package.json doesn't have module part at all.


Solution

  • IDE in this case, misdirect me a bit. Almost forgot, that I am dealing with TS now. Some modules seem to have no default exports, so: