Cannot find name 'require' after upgrading to Angular4
Still not sure the answer, but a possible workaround is
import * as Chart from 'chart.js';
Finally I got solution for this, check my App module file :
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from '@angular/material';
import 'hammerjs';
import { ChartModule } from 'angular2-highcharts';
import * as highcharts from 'highcharts';
import { HighchartsStatic } from 'angular2-highcharts/dist/HighchartsService';
import { AppRouting } from './app.routing';
import { AppComponent } from './app.component';
declare var require: any;
export function highchartsFactory() {
const hc = require('highcharts');
const dd = require('highcharts/modules/drilldown');
dd(hc);
return hc;
}
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRouting,
BrowserAnimationsModule,
MaterialModule,
ChartModule
],
providers: [{
provide: HighchartsStatic,
useFactory: highchartsFactory
}],
bootstrap: [AppComponent]
})
export class AppModule { }
Notice declare var require: any;
in the above code.
Will work in Angular 7+
I was facing the same issue, I was adding
"types": ["node"]
to tsconfig.json of root folder.
There was one more tsconfig.app.json under src folder and I got solved this by adding
"types": ["node"]
to tsconfig.app.json file under compilerOptions
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"types": ["node"] ----------------------< added node to the array
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
The problem (as outlined in typescript getting error TS2304: cannot find name ' require') is that the type definitions for node are not installed.
With a projected genned with @angular/cli 1.x
, the specific steps should be:
Step 1:
Install @types/node
with either of the following:
- npm install --save @types/node
- yarn add @types/node -D
Step 2:
Edit your src/tsconfig.app.json file and add the following in place of the empty "types": []
, which should already be there:
...
"types": [ "node" ],
"typeRoots": [ "../node_modules/@types" ]
...
If I've missed anything, jot a comment and I'll edit my answer.