V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
mikulch
V2EX  ›  问与答

刚学习编程就遇到 Cannot find module 'Buffer' or its corresponding type declarations

  •  
  •   mikulch · 2022-05-25 16:56:59 +08:00 · 1286 次点击
    这是一个创建于 702 天前的主题,其中的信息可能已经有所发展或是发生改变。

    tsconfig.json

    {
      "compilerOptions": {
        "target": "esnext", // 指定 ECMAScript 的目标版本
        "module": "esnext", // 指定编译生成哪个模块的系统代码,
        "outDir": "dist", // 编译输出目录,即 .ts 文件编译成 .js 文件后的输出目录。这里设置为根目录下的 /dist 目录
        "strict": true, // 严格模式
        "noImplicitAny": false, // 在表达式和声明上有隐含的 any 类型时报错。设为 false 避免当类型推论为 any 时报错
        "moduleResolution": "node", // 决定如何处理模块。设置为 node
        "baseUrl": "./", // 定义 ts 项目的根目录,设置 paths 前必须设置
        //定义路径别名,即当我们通过路径引入一个模块时,可以使用别名来进行引入,这里第一个 * 设置是为了引入第三方模块; 第二个 '@/*' 则是为了直接快捷的导入 /src 下的模块。
        "paths": {
          "*": ["node_modules/*", "src/types/*"],
          "@/*": ["src/*"]
        },
        "esModuleInterop": true,
        "skipLibCheck": true /* Skip type checking all .d.ts files. */,
        "sourceMap": true,
        "types": ["node"]
      }
    }
    
    

    package.json

    {
      "name": "node-test",
      "version": "0.0.1",
      "description": "",
      "license": "MIT",
      "author": "xxx",
      "type": "module",
      "main": "index.js",
      "scripts": {
        "clean": "tsc --build --clean",
        "build": "tsc",
        "start": "node --es-module-specifier-resolution=node dist/index.js",
        "test": "mocha test/**/*.ts"
      },
      "dependencies": {},
      "devDependencies": {
        "@faker-js/faker": "^6.3.1",
        "@types/chai": "^4.3.1",
        "@types/mocha": "^9.1.1",
        "@types/node": "^17.0.35",
        "@types/sinon": "^10.0.11",
        "@types/sinon-chai": "^3.2.8",
        "@typescript-eslint/eslint-plugin": "^5.22.0",
        "@typescript-eslint/parser": "^5.22.0",
        "chai": "^4.3.6",
        "eslint": "^8.14.0",
        "eslint-config-standard": "^17.0.0",
        "eslint-plugin-import": "^2.26.0",
        "eslint-plugin-n": "^15.2.0",
        "eslint-plugin-promise": "^6.0.0",
        "mocha": "^10.0.0",
        "sinon": "^13.0.2",
        "sinon-chai": "^3.7.0",
        "ts-node": "^10.8.0",
        "typescript": "^4.6.4"
      }
    }
    
    

    本地 node -v

    v14.19.1

    服务器 node -v

    v14.19.3

    问题

    本地编译运行都没有问题。代码拷贝到服务器以后,npm run build 出现错误:

    Cannot find module 'Buffer' or its corresponding type declarations.
    2 import Buffer from "Buffer";
                         ~~~~~~~~
    Found 1 error in service/core.ts:2
    

    在网上搜了一天了,查到了类似的问题: https://stackoverflow.com/questions/38875401/getting-error-ts2304-cannot-find-name-buffer

    但是最终还是没解决。求大佬帮忙!

    7 条回复    2022-05-26 09:27:31 +08:00
    libook
        1
    libook  
       2022-05-25 17:53:04 +08:00   ❤️ 1
    你在服务器上有执行 npm install 吗?
    错误写得比较清晰,说找不到 Buffer 或找不到 Buffer 的类型定义,那么极有可能是你服务器上没有安装 @type/node 包,这个包就是包含 Node 的所有 API 的类型定义文件给 TS 编译器用的。

    想学的话还是建议从纯 JS 的 Node 开始,理解了 Node 的工作原理之后再考虑引入 TS ,这样循序渐进,毕竟 TS 不是只为 Node 提供的,Node 也不一定非要用 TS 。

    Node 的官方文档有 Guides ,也写得挺清晰的,看英文不方便也可以找中文翻译版,看完也花不了太多时间。
    zhangxh1023
        2
    zhangxh1023  
       2022-05-25 17:58:20 +08:00
    你是想要用 Buffer? Buffer 是不需要引入的呀,全局的,就算要引入 也是 buffer 小写的

    https://nodejs.org/dist/latest-v16.x/docs/api/buffer.html#buffer
    mikulch
        3
    mikulch  
    OP
       2022-05-25 18:01:46 +08:00
    @zhangxh1023 还真是这个问题,不过我引入了 buffer 小写的编译不通过,引入的不是这个实际使用的 buffer 。我已经把这个删除了。
    zhangxh1023
        4
    zhangxh1023  
       2022-05-25 18:04:44 +08:00
    @mikulch 不是实际引用的? dependencies 里面你也没声明额外的第三方的包呀,你这个 Buffer ,原本是想要用啥?搜了一下 npm 也没找到有叫 Buffer 的包
    mikulch
        5
    mikulch  
    OP
       2022-05-25 18:06:46 +08:00
    @zhangxh1023 就是想要全局的那个 Buffer ,操作二进制数据的那个类。应该是之前 import 错了。但是改成小写确实不行。

    Property 'concat' does not exist on type 'typeof import("buffer")'.

    51 const body = Buffer.concat(chunks);
    des
        6
    des  
       2022-05-25 19:56:15 +08:00 via iPhone
    @mikulch buffer 不需要 import 啊,你把 service/core.ts 第二行删掉再试试
    zhangxh1023
        7
    zhangxh1023  
       2022-05-26 09:27:31 +08:00
    @mikulch 你 import 错了应该
    import {Buffer} from 'buffer';
    注意 大括号,你可以 console.log 看一下 你 import 的是什么

    或者干脆不 import
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2593 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:46 · PVG 23:46 · LAX 08:46 · JFK 11:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.