我试图让我的第一个Typescript和DefinitelyTyped节点应用程序启动并运行,并遇到一些错误.
当我尝试转换一个简单的ts节点页面时,我收到错误"TS2304:找不到名称'require'".我已经在SO上阅读了其他几个这样的错误,我认为我没有类似的问题.
我在shell提示符下运行命令:tsc movie.server.model.ts.该文件的内容是:
tsc movie.server.model.ts.
Run Code Online (Sandbox Code Playgroud)
var mongoose = require('mongoose')行引发错误
typings/tsd.d.ts文件的内容是:
'use strict';
/// <reference path="typings/tsd.d.ts" />
/* movie.server.model.ts - definition of movie schema */
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var foo = 'test';
Run Code Online (Sandbox Code Playgroud)
.d.ts文件引用放在相应的文件夹中,并通过命令添加到typings/tsd.d.ts:
/// <reference path="node/node.d.ts" />
/// <reference path="requirejs/require.d.ts" />
Run Code Online (Sandbox Code Playgroud)
生成的.js文件似乎工作正常,所以我可以忽略错误.但我很想知道为什么会出现这种错误以及我做错了什么.
我不能在 typescript 3.2 中使用任何类型的 @type/node
我的 tsconfig :
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"skipLibCheck": true,
"suppressImplicitAnyIndexErrors": true,
"outDir": "target/www/app",
"lib": ["es7", "dom"],
"baseUrl": "./",
"paths": {
"app/*": ["src/app/*"]
},
"types": ["node"],
"importHelpers": true,
"allowJs": true
}
}
Run Code Online (Sandbox Code Playgroud)
打字稿版本:3.2.2 @type/node 版本:11.13.5
我有这个错误信息:
错误 TS2580:找不到名称“进程”。你需要为节点安装类型定义吗?尝试
npm i @types/node然后添加node到 tsconfig 中的 types 字段。
我理解错误,但没有找到修复它的方法。
更新 :
npm ls --depth=0
+-- @angular-devkit/build-angular@0.13.8
+-- @angular/animations@7.2.13
+-- @angular/cli@7.3.8
+-- …Run Code Online (Sandbox Code Playgroud)