Angular-cli: Cannot find name 'require'

Created on 9 Mar 2017  Â·  10Comments  Â·  Source: angular/angular-cli

Hi, angular-cli update to @angular/cli: 1.0.0-rc.1 An error has occurred . pls help  

npm

npm install crypto-js
npm install @types/crypto-js
typings install dt~crypto-js

app.component.ts
import { Component } from '@angular/core';

var CryptoJS = require('crypto-js');

ng serve

ERROR in C:/Users/Jino/Documents/ngProject/slb/src/app/app.component.ts (3,16): Cannot find name 'require'.)

C:\Users\Jino\DocumentsngProject\slb>ng -v
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ â–³ \ | '_ \ / _| | | | |/ _ | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ __| |_|__, |__,_|_|__,_|_| ____|_____|___|
|___/
@angular/cli: 1.0.0-rc.1
node: 7.7.2
os: win32 x64
@angular/common: 2.4.9
@angular/compiler: 2.4.9
@angular/core: 2.4.9
@angular/forms: 2.4.9
@angular/http: 2.4.9
@angular/platform-browser: 2.4.9
@angular/platform-browser-dynamic: 2.4.9
@angular/router: 3.4.9
@angular/cli: 1.0.0-rc.1
@angular/compiler-cli: 2.4.9

Most helpful comment

I don't want to use require either. Is there a better way for importing a localization-file (.json), which is included in a npm-package?

All 10 comments

I wouldn't encourage importing packages using require, but if you need to make sure you've added node to the types array in .angular-cli.json.

@filipesilva
I can not find any information about an types array in .angular-cli.json
Did you mean the types array in tsconfig.json or do you have any further information?

@sven42 types goes in the tsconfig.json

Why not copy an example of what you mean exactly for those of us getting the same error? Have no idea what you are talking about but getting the same error

Apologies, it indeed meant tsconfig.json instead of .angular-cli.json. More specifically, src/tsconfig.app.json. It has a "types": [] entry, where you can add more types (such as "node"). Again though, I don't suggest doing it.

I don't want to use require either. Is there a better way for importing a localization-file (.json), which is included in a npm-package?

@filipesilva Why do you recommend against using require and what is a better alternative ?

in src folder open tsconfig.app.json and add
"compilerOptions": {
...
"types": [
"node"
]

},

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings