143

I want to use Chart.js within my Angular project. In previous Angular2 versions, I have been doing this well by using a 'chart.loader.ts' which has:

export const { Chart } = require('chart.js');

Then in the component code I just

import { Chart } from './chart.loader';

But after upgrading to cli 1.0.0 and Angular 4, I get the error: "Cannot find name 'require'".

To reproduce the error:

ng new newapp
cd newapp
npm install chart.js --save
echo "export const { Chart } = require('chart.js');" >> src/app/chart.loader.ts
ng serve

In my 'tsconfig.json', I have

"typeRoots": [
  "node_modules/@types"
],

And in 'node_modules/@types/node/index.d.ts' there is:

declare var require: NodeRequire;

So I'm confused.

BTW, I constantly encounter the warning:

[tslint] The selector of the component "OverviewComponent" should have prefix "app"(component-selector)

Though I have set the "prefix": "" in my '.angular-cli.json'. Could it because changing from 'angular-cli.json' to '.angular-cli.json' the cause?

3
  • The problem is in your tsconfig.json file. See the solution here: stackoverflow.com/questions/31173738/… Commented Mar 30, 2017 at 19:17
  • @IndyWill Thanks, actually it should be in src/tsconfig.app.json. Can you write this as an answer? Commented Mar 30, 2017 at 19:49
  • For electron + angular 2/4 see: stackoverflow.com/a/47364843/5248229 Commented Nov 18, 2017 at 10:16

8 Answers 8

265

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.

Sign up to request clarification or add additional context in comments.

8 Comments

well didnt work for me... do i have to install something else?
Also for me it doesn't work. Details here: stackoverflow.com/questions/44421367/types-not-found
Note: You have to change tsconfig.app.json under src folder adding "types": ["node"], it is not in the root tsconfig
Didn't work for me either. another answer suggesting just to add declare var require: any; helped surprisingly.
it worked for me but I have to remove "typeRoots": [ "../node_modules/@types" ], I am using Angular 5.
|
41

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"
  ]
}

2 Comments

simple and easy, solved my problem angular 12
The sconfig.app.json file was what I was missing (Angular 14). Thank you!
25

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.

1 Comment

I had this issue in the polyfills.ts file, but your "declare var require: any;" fixed it. thanks
16

Still not sure the answer, but a possible workaround is

import * as Chart from 'chart.js';

Comments

16

As for me, using VSCode and Angular 5, only had to add "node" to types in tsconfig.app.json. Save, and restart the server.

{
    "compilerOptions": {
    ..
    "types": [
      "node"
    ]
  }
  ..
}

One curious thing, is that this problem "cannot find require (", does not happen when excuting with ts-node

3 Comments

Worked like a charm in Angular 6 for me.
This solution worked for me in an Angular 6 library. I needed to add the "types": [ "node" ], to the tsconfig.lib.json though.
AND RESTART SERVER. My goodness, it was this all along. Working on Angular 9. 👍
4

I added

"types": [ 
   "node"
]

in my tsconfig file and its worked for me tsconfig.json file look like

  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "types": [ 
      "node",
      "underscore"
    ]
  },  

1 Comment

While this code might answer the question you still might consider adding a few explanatory sentences as this increases the value of your answer for other users.
3

Add the following line at the top of the file that gives the error:

declare var require: any

Comments

0

I moved the tsconfig.json file into a new folder to restructure my project.

So it wasn't able to resolve the path to node_modules/@types folder inside typeRoots property of tsconfig.json

So just update the path from

"typeRoots": [
  "../node_modules/@types"
]

to

"typeRoots": [
  "../../node_modules/@types"
]

To just ensure that the path to node_modules is resolved from the new location of the tsconfig.json

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.