1

I have an angular 2 app generated using angular cli. I am using typescript. It fails when I import redux-logger using:const createLogger = require('redux-logger');

The error is:

The Broccoli Plugin: [BroccoliTypeScriptCompiler] failed with:
Error: Typescript found the following errors:
/Users/abdul.badru/2016/Development/BudgetSoftware/app/budgetapp/tmp/broccoli_type_script_compiler-input_base_path-eQlgH862.tmp/0/src/app/app.module.ts (24, 22): Cannot find name 'require'.
/Users/abdul.badru/2016/Development/BudgetSoftware/app/budgetapp/tmp/broccoli_type_script_compiler-input_base_path-eQlgH862.tmp/0/src/app/store/index.ts (5, 22): Cannot find name 'require'.

What is going on here? What I am missing?

7
  • import createLogger from "redux-logger"? Commented Sep 1, 2016 at 12:48
  • I have tried, but when I do that now it says: Cannot find module 'redux-logger'. Commented Sep 1, 2016 at 12:56
  • Try npm install --save redux-logger and see if it works. Commented Sep 1, 2016 at 13:01
  • I already did npm install but I have done it again. Typescript is still giving me the same error: Cannot find module 'redux-logger'. But when I put const createLogger = require('redux-logger'); and point the cursor over require it says Corresponding file is not included in tsconfig.json Commented Sep 1, 2016 at 13:05
  • Hi Abdul. Did you specifically run npm install --save redux-logger or did you only run npm install? Commented Sep 1, 2016 at 13:09

2 Answers 2

2

The solution is to add on typings.d.ts file the following declaration:

declare var require: any;

And now I can require:

const createLogger = require('redux-logger');
Sign up to request clarification or add additional context in comments.

1 Comment

I put this declaration atop the require and all worked fine
0

Require is a global namespace and it isn't properly defined. You can install it's definition file with the typings installer, if you're using that:

typings install --global --save-dev dt~require

Or you could add the typing file manually. The file can be found here.

Or you can tell the Typescript compilier that require exists and can be anything by putting the following in your file or your global .d.ts file (index.d.ts, typings.d.ts):

declare var require: any;

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.