Nestjs:导入模块未定义,但可以导入模块中的方法和函数 [英] Nestjs: import modules undefined, but methods and functions from modules can be imported

查看:40
本文介绍了Nestjs:导入模块未定义,但可以导入模块中的方法和函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Nestjs 和 WebStorm &TS 4.2.3^最新.

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

我面临的问题有点奇怪.例如,一些模块,如 axios 可以像往常一样安装、导入和使用.但是某些模块,尤其是 Nodejs Core,例如 fspath,不能作为模块导入.但是他们的方法可以导入并使用就好了!

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';

我敢肯定,他们有正确的 TS 类型,甚至手动安装.例如:

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!
  }
}

我只有一个由 Nest Cli 生成的 tsconfig.json.我正在通过 npm start:dev -name 启动我的应用程序,IDE 不会显示任何代码错误,直到我运行代码.

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.

tsconfig.json 模块部分,只是为了确定:"module":"commonjs",package.json 根本没有 module 部分.

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

推荐答案

IDE 在这种情况下,误导了我一点.差点忘了,我现在正在处理 TS.某些模块似乎没有默认导出,因此:

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:

  • 你应该像他们一样导入:import * as fs from 'fs';
  • 或者,另一个选项是启用:"esModuleInterop": true, 在您的 tsconfig.json

这篇关于Nestjs:导入模块未定义,但可以导入模块中的方法和函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆