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

Missing require-context for Webpack #6170

Closed
Ridermansb opened this issue Oct 6, 2015 · 19 comments
Closed

Missing require-context for Webpack #6170

Ridermansb opened this issue Oct 6, 2015 · 19 comments

Comments

@Ridermansb
Copy link
Contributor

Hi @tkqubo .. thanks for your contribution.

Just one feedback:

ERROR tests.webpack.ts
error TS2339: Property 'context' does not exist on type 'NodeRequire'.

ERROR tests.webpack.ts
error TS2339: Property 'keys' does not exist on type 'IContextDefinition'.

Missing require-context definition:

Example tests.webpack.ts file for test

var context = require.context('./test', true, /.+\.spec\.js?$/);
context.keys().forEach(context);
@tkqubo
Copy link
Contributor

tkqubo commented Oct 7, 2015

I think those definitions were added yesterday by this pr #6086
In addition to webpack.d.ts, you need to refer webpack-env.d.ts and the lines below can compile

/// <reference path="./webpack.d.ts" />
/// <reference path="./webpack-env.d.ts" />

var context = require.context('./test', true, /.+\.spec\.js?$/);
context.keys().forEach(context);

@Ridermansb
Copy link
Contributor Author

I have add in tsconfig.json file using..

...
"filesGlob": [
    "./**/*.ts"
],

How do I get these files?
I installed the definition using:

tsd update webpack -riso

And only /// <reference path="webpack/webpack.d.ts" /> is available

@tkqubo
Copy link
Contributor

tkqubo commented Oct 7, 2015

You can fetch webpack-env.d.ts by

tsd install webpack-env -riso

or... should it be combined into single webpack.d.ts file?

@Ridermansb
Copy link
Contributor Author

I think that can be combined into single file :)

Thanks 👍

@tkqubo
Copy link
Contributor

tkqubo commented Oct 8, 2015

My pleasure! 🎸

@tkqubo
Copy link
Contributor

tkqubo commented Oct 15, 2015

@Ridermansb Hi, I recently merged webpack-env.* into webpack ,but reverted it again since they should be used in a different context.
c.f. #6243 (comment)

So if you want to refer require.context, you need to get webpack-env.d.ts independently of webpack.d.ts after the revert will be merged.

Thank you

@ozyman42
Copy link

How can I get webpack-env.d.ts installed into my project via the typings command?

@tkqubo
Copy link
Contributor

tkqubo commented Jun 28, 2016

Could you try this?

typings install dt~webpack-env --global

@ozyman42
Copy link

It works! Thanks!

@umasgn
Copy link

umasgn commented Aug 11, 2016

error TS2307: Cannot find module 'uglify-js'.
err

@ozyman42
Copy link

Peer dependencies are not automatically installed by typings. You need to install those yourself. Run typings install --save --global dt~uglify-js

@jrgleason
Copy link

I just tried npm install --save-dev @types/webpack @types/webpack-env and I still get...

ERROR in /Users/.../code/webpack-test/node_modules/@types/webpack-env/index.d.ts
(183,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'require' must be of type 'NodeRequire', but here has type 'RequireFunction'.

ERROR in /Users/.../code/webpack-test/node_modules/@types/webpack-env/index.d.ts
(232,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'module' must be of type 'NodeModule', but here has type 'Module'.

ERROR in ./test/test.bundle.ts
(5,35): error TS2339: Property 'context' does not exist on type 'NodeRequire'.

@zzczzc004
Copy link

zzczzc004 commented Nov 23, 2016

@jrgleason , I also have the same problem, it show there are two variables(require, module) conflicted in Node.JS and webpack-env. I think we should change the variables' name or not put the variable to global. I think this problem is the same as 8018 and 11324.

(73,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'require' must be of type 'RequireFunction', but here has type 'NodeRequire'.
(85,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'module' must be of type 'Module', but here has type 'NodeModule'.

@nathanosdev
Copy link

@zzczzc004 It seems you are also loading types for node, webpack-env types should be loaded instead of node types, not in addition to them.

@sjpurol
Copy link
Contributor

sjpurol commented Jan 26, 2017

@zzczzc004, @types/webpack includes @types/node as a dependency, thus installing both @types/webpack and @types/webpack-env resulted in the errors you described. Try only installing @types/webpack-env. This resolved the issue for me. 👍

@aalenliang
Copy link

I'm using node require and webpack require, I've installed
@types/node
@types/webpack
@types/webpack-env

however, using 'require.context' I still got
error TS2339: Property 'context' does not exist on type 'NodeRequire'.

Any ideas?

@aneurysmjs
Copy link

@alexleung @tkqubo by installing the typings definitions solved the problem for me

typings install dt~webpack-env --global

@rnons
Copy link

rnons commented Jul 4, 2017

Isn't wepack-env for the version 1.13

// Type definitions for webpack (module API) 1.13

Shouldn't RequireContext be added to types/webpack/index.d.ts as well?

@dennisat
Copy link

npm install --save-dev @types/webpack-env

and in tsconfig.json add it in the types property
example:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "declaration": true,
    "declarationDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "target": "es6",
    "allowJs": false,
    "types": [
      "node",
      "webpack-env"
    ]
  },
  "files": [
    "src/index.ts"
  ],
  "compileOnSave": false
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests