12

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'; // working fine
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  async test(input: string): Promise<void> {
    await axios.get() // working fine

    await path.join() // Cannot read property 'join' of undefined

    //BUT 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 don't show any errors in code, until I ran code.

Example

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

AlexZeDim
  • 3,520
  • 2
  • 28
  • 64

1 Answers1

38

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:

  • You should import as with them: import * as fs from 'fs';
  • Or, another option is enabling: "esModuleInterop": true, in your tsconfig.json
AlexZeDim
  • 3,520
  • 2
  • 28
  • 64