Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find name 'require'. using relative paths... #2221

Closed
born2net opened this issue Sep 19, 2016 · 14 comments
Closed

Cannot find name 'require'. using relative paths... #2221

born2net opened this issue Sep 19, 2016 · 14 comments

Comments

@born2net
Copy link

I am using a config with relative paths for style and template:

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'app works...!';
    ...

and getting an error of

Time: 3117ms
            Asset     Size  Chunks       Chunk Names
   main.bundle.js  4.62 MB    0, 3       main
scripts.bundle.js   330 kB    1, 3       scripts
 styles.bundle.js  10.2 kB    2, 3       styles
        inline.js  5.53 kB       3       inline
         main.map  5.04 MB    0, 3       main
      scripts.map   413 kB    1, 3       scripts
       styles.map    14 kB    2, 3       styles
       inline.map  5.59 kB       3       inline

ERROR in [default] C:\msweb\ng2Boilerplate\src\app\app.component.ts:45:14
Cannot find name 'require'.

ERROR in [default] C:\msweb\ng2Boilerplate\src\app\app.component.ts:46:13
Cannot find name 'require'.
Child html-webpack-plugin for "index.html":
         Asset    Size  Chunks       Chunk Names
    index.html  3.3 kB       0
webpack: bundle is now VALID.

running:

angular-cli: 1.0.0-beta.14
node: 6.5.0
os: win32 x64

regards

Sean

@ghost
Copy link

ghost commented Sep 19, 2016

Same here,

strangely enough the application still seems to work.

@ghost
Copy link

ghost commented Sep 19, 2016

Ahhh here we go,

try adding
declare var require: any;

To src/typings.d.ts

@born2net
Copy link
Author

ha great!!!
might be good to add to the included default d.ts file

@jesselpalmer
Copy link
Member

@hansl I had to add declare var require: any; to the src/typings.d.ts file as well to get the errors to disappear, even though the application appeared to be working ok. Is this something that should be added permanently to the typings.d.ts file?

@blackat
Copy link

blackat commented Sep 25, 2016

Hi,
I have the same issue but I do not use typings, I am using @type via npm. I have created the file src/typings.d.ts and added the declare var require: any;. The error disappear, but any good reason to do that? is there a better solution?

Thanks in advance 😉

@ghost
Copy link

ghost commented Sep 25, 2016

It's just telling the typescript compiler that there is a global function
called require. And to not freak out about references to it.

On Sun, Sep 25, 2016, 1:53 PM Eugenio Lentini notifications@github.com
wrote:

Hi,
I have the same issue but I do not use typings, I am using @type via npm.
I have created the file src/typings.d.ts and added the declare var
require: any;. The error disappear, but any good reason to do that? is
there a better solution?

Thanks in advance 😉


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#2221 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABkM7BLICzfJKt4gvlHZ-lgsXLAFyyDAks5qtrUqgaJpZM4KAjRR
.

peterblazejewicz added a commit to peterblazejewicz/real-world-app-client that referenced this issue Oct 14, 2016
@peterblazejewicz
Copy link
Contributor

FYI: I've fixed the original problem by adding @types/node with:

npm install @types/node

@Kumabhiman
Copy link

Add typing.d.ts in main folder of the application and over there declare the varible which you want to use every time

As for example:

declare var System: any;
declare var require: any;

after declaring this in typing.d.ts, error for "require" and "system" will not come in the application..

@wfsovereign
Copy link

I fix it, by try adding

declare var require: NodeRequire;

To src/typings.d.ts, hope it can help you

@stephengeorgewest-navitaire

When I used:

declare var require: any;

Npm functioned but in visual studio I got:

Error	TS2403	Subsequent variable declarations must have the same type.  Variable 'require' must be of type 'any', but here has type 'NodeRequire'.	TypeScript Virtual Projects	node_modules\@types\node\index.d.ts	

When I used:

declare var require: NodeRequire;

npm and visual studio failed on:

ERROR in src/typings.d.ts (18,22): Cannot find name 'NodeRequire'.

Adding to tsconfig.json didn't help:

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

I ended up dumping a bunch of stuff in src/typings.d.ts

interface NodeRequireFunction {
	(id: string): any;
}

interface NodeRequire extends NodeRequireFunction {
	resolve(id: string): string;
	cache: any;
	extensions: any;
	main: NodeModule | undefined;
}

declare var require: NodeRequire;

interface NodeModule {
	exports: any;
	require: NodeRequireFunction;
	id: string;
	filename: string;
	loaded: boolean;
	parent: NodeModule | null;
	children: NodeModule[];
}

declare var module: NodeModule;

@pgscada
Copy link

pgscada commented Jun 18, 2017

For me it was a typeRoots issue, need to ensure that it points correctly to node_modules if tsconfig is in the /src/ folder.

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

@odahcam
Copy link

odahcam commented Dec 11, 2017

I had a duplicated type and that was causing the same issue.

@kover
Copy link

kover commented Feb 21, 2018

I had a similar issue and the error is gone after I removed

"types": []

from tsconfig.app.json which in /src/ folder or you can add

"types": ["node"]

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants