凯凯本凯 2023-01-30 08:51 采纳率: 72.5%
浏览 18
已结题

ts-node 无法导入 node_modules

import qs from 'qs'; console.log(qs); 打印出来是 undefined

确定安装了 qs@types/qs

package.json

{
  "name": "fanbook-api-sdk",
  "version": "0.1.0",
  "description": "fanbook OpenAPI SDK",
  "main": "dist/main.js",
  "author": "Starlight-Dev-Team",
  "license": "MIT",
  "private": false,
  "scripts": {
    "dev": "ts-node -r tsconfig-paths/register ./src/main.ts --files",
    "build": "rm -rf ./dist && tsc"
  },
  "dependencies": {
    "@types/qs": "^6.9.7",
    "axios": "^1.2.6",
    "eslint": "^8.33.0",
    "qs": "^6.11.0",
    "ts-node": "^10.9.1",
    "typescript": "*"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "eslint-config-standard-with-typescript": "^33.0.0",
    "eslint-plugin-import": "^2.25.2",
    "eslint-plugin-n": "^15.0.0",
    "eslint-plugin-promise": "^6.0.0",
    "tsconfig-paths": "^4.1.2"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "moduleResolution": "node",
    "typeRoots": [
      "node_modules/@types"
    ],
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "alwaysStrict": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    },
    "rootDir": "./src",
    "outDir": "./dist"
  },
  "include": [
    "./src/*"
  ],
  "exclude": [
    "node_modules",
    "dist"
  ]
}
  • 写回答

2条回答 默认 最新

  • CodeBytes 2023-01-30 16:46
    关注

    看起来是 TypeScript 的配置问题,请检查你的 tsconfig.json 中是否设置了 baseUrl,paths 和 rootDir。可能需要重新定义您的 "paths" 以便 TypeScript 正确解析第三方库的类型定义。

    您还可以尝试将下面的行添加到您的代码中:

    import * as qs from 'qs';
    console.log(qs);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月7日
  • 已采纳回答 1月30日
  • 创建了问题 1月30日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测