簡體   English   中英

在TypeScript中導入節點模塊

[英]Importing Node modules in TypeScript

如何導入位於TypeScript中node_modules文件夾中的Node模塊?

當我嘗試編譯以下的TypeScript代碼時,我收到一條錯誤消息( 當前作用域中不存在名稱''async'' ):

// Converted from: var async = require('async');
import async = module('async');

您需要創建一個定義文件並包含以下內容:

module "async" {}

然后在TypeScript代碼中添加對此定義文件的引用

///<reference path='definition-file.d.ts' />

import async = module('async');

打字稿中的標准“import blah = require('x')”語義不適用於節點模塊。

解決此問題的最簡單方法是明確定義和接口並使用require,而不是導入:

// You can put this in an external blah.d file if you like; 
// remember, interfaces do not emit any code when compiled.
interface qOrm {
    blah:any;
}

declare var require:any;
var qOrm:qOrm = require("../node_modules/q-orm/lib/q-orm");

var x = qOrm.blah;
npm install --save-dev @types/async
npm install --save async

語法:

import * as async from "async"

暫無
暫無

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

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