Angular: 不能使用 'require' 关键字
Angular: Can't use 'require' keyword
在我的应用程序中,我需要使用 require
关键字来导入某些内容。但我不能。它显示以下错误:
ERROR in src/app/components/test1/test1.component.ts:3:24 - error TS2591: Cannot find name 'require'. Do you need
to install type definitions for node? Try `npm i @types/node` and then add `node` to the types field in your tsconfig.
3 const {Pool, Client} = require('pg');
test1.component.ts
import { Component, OnInit } from '@angular/core';
const {Pool, Client} = require('pg');
const connectionString = 'postgressql;://postgres:1@localhost:3000/test1';
const client = new Client({connectionString});
client.connect();
client.query('select * from posts',
(err, res) => {console.log(err, res);
client.end();
});
@Component({
selector: 'app-test1',
templateUrl: './test1.component.html',
styleUrls: ['./test1.component.css']
})
export class Test1Component implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"lib": [
"es2018",
"dom"
],
"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
请指出哪里出了问题
[ 注意一点,对于这部分错误
Try npm i @types/node
and then add node
to the types field in your
tsconfig.
我安装了 npm @types/node --save
然后将行添加到 tsconfig.json 但错误保持不变 ]
不管你是否可以在浏览器中使用 postgresql(你不能)。要导入这样的包,您必须安装类型,然后您可以使用 import
语句:
npm i -D @types/pg
import { Pool, Client } from 'pg';
让我再说一遍,包 pg
仅适用于 node.js
环境,不适用于浏览器:
Non-blocking PostgreSQL client for Node.js
在我的应用程序中,我需要使用 require
关键字来导入某些内容。但我不能。它显示以下错误:
ERROR in src/app/components/test1/test1.component.ts:3:24 - error TS2591: Cannot find name 'require'. Do you need
to install type definitions for node? Try `npm i @types/node` and then add `node` to the types field in your tsconfig.
3 const {Pool, Client} = require('pg');
test1.component.ts
import { Component, OnInit } from '@angular/core';
const {Pool, Client} = require('pg');
const connectionString = 'postgressql;://postgres:1@localhost:3000/test1';
const client = new Client({connectionString});
client.connect();
client.query('select * from posts',
(err, res) => {console.log(err, res);
client.end();
});
@Component({
selector: 'app-test1',
templateUrl: './test1.component.html',
styleUrls: ['./test1.component.css']
})
export class Test1Component implements OnInit {
constructor() { }
ngOnInit(): void {
}
}
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"lib": [
"es2018",
"dom"
],
"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
请指出哪里出了问题
[ 注意一点,对于这部分错误
Try
npm i @types/node
and then addnode
to the types field in your tsconfig.
我安装了 npm @types/node --save 然后将行添加到 tsconfig.json 但错误保持不变 ]
不管你是否可以在浏览器中使用 postgresql(你不能)。要导入这样的包,您必须安装类型,然后您可以使用 import
语句:
npm i -D @types/pg
import { Pool, Client } from 'pg';
让我再说一遍,包 pg
仅适用于 node.js
环境,不适用于浏览器:
Non-blocking PostgreSQL client for Node.js