簡體   English   中英

將 Node.js SDK 導入在節點服務器中運行的 Typescript 中出現錯誤“ReferenceError:require is not defined”

[英]Importing Node.js SDK into Typescript running in Node server gave error “ReferenceError: require is not defined”

我正在研究 Node.js SDK 以供服務器端使用,並在嘗試使用 Z9327DAFFB330417F817934CC99F5048 將其導入我的快速應用程序時遇到此問題“ ReferenceError: require is not defined 最初我沒有生成 .d.ts 文件,所以我導入了 Typescript 並生成了聲明文件,但仍然出現錯誤。 我在 SDK package 中的 index.js:

const myDep = require('./myDep');
const client = function (sdkKey) {
    const c = {};
    c.do = (user) => {};
    return c;
}

module.exports = client;

生成的 index.d.ts 文件如下所示:

export = client;
declare function client(sdkKey: any): {
    do(user: any): void;
};

這是我的 SDK package 中的 tsconfig.json:

{
  "include": ["src/**/*", "src/index.d.ts"],
  "compilerOptions": {
    "resolveJsonModule": true,
    "target": "es6",
    "lib": ["es6"],
    "module": "commonjs",
    "allowJs": true,
    "checkJs": true,
    "declaration": true,
    "emitDeclarationOnly": true,
    "moduleResolution": "Node",
    "outDir": "./dist"
  }
}

我正在使用 npm 鏈接將其導入運行 typescript 的節點服務器中,並在我的 app.ts 文件中使用導入

import * as mySDK from 'my-node-js-sdk';

最后想通了,並通過添加 index.d.ts 文件進行類型聲明來修復它。 在我的項目中還發現了幾個地方,我使用 ES6 導出而不是模塊,因此也必須修復這些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM