angular2项目需要调用外部nodejs模块,没有ts版本,记录一下方法
TypeScript 2.x
If you are using TypeScript 2.x you no longer need to have Typings or Definitely Typed installed. Simply install the following package.
npm install @types/node --save-dev
The Future of Declaration Files (6/15/2016)
Tools like Typings and tsd will continue to work, and we’ll be working alongside those communities to ensure a smooth transition.
OPTIONAL: If you are specifying typeroots or types in your tsconfig.json. You may need to update the tsconfig.json to include node as a type. By default any package under @types is already included in your build unless you've specified either of these options. Read more
Below is proper configuration for each of these options.
{
"compilerOptions": {
// types option has been previously configured
"types": [
// add node as an option
"node"
],
// typeRoots option has been previously configured
"typeRoots": [
// add path to @types
"node_modules/@types"
]
}
}
TypeScript 1.x
Using typings (DefinitelyTyped's replacement) you can specify a definition directly from a GitHub repository.
Install typings
npm install typings -g --save-dev
Install the requireJS type definition from DefinitelyType's repo
typings install dt~node --save --global
Quick and Dirty
If you just have one file using require, or you're doing this for demo purposes you can define require at the top of your TypeScript file.
declare var require: any
Angular CLI
If you're attempting to use a require statement in your Angular CLI project, update tsconfig.app.json to include node typings.
"compilerOptions": {
// other options
"types": [
"node"
]
}
本文介绍如何在Angular2项目中使用Node.js模块的方法,包括安装必要的TypeScript定义文件,配置tsconfig.json文件,以及在AngularCLI项目中正确引用Node.js模块。
612

被折叠的 条评论
为什么被折叠?



