Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Cannot read property 'context' of undefined #125

Closed
duan602728596 opened this issue Feb 18, 2018 · 2 comments
Closed

Cannot read property 'context' of undefined #125

duan602728596 opened this issue Feb 18, 2018 · 2 comments

Comments

@duan602728596
Copy link

duan602728596 commented Feb 18, 2018

When I use webpack (version: 4.0.0-beta.2) and worker-loader (version: next), it has an error:

ERROR in ./node_modules/worker-loader/dist/cjs.js?name=worker!./src/worker.js
Module build failed: TypeError: Cannot read property 'context' of undefined
    at Object.pitch (E:\gongcheng\qqtools\worker-loader\ceshi\node_modules\worker-loader\dist\index.js:71:45)
 @ ./src/index.js 1:0-56

I found the error here: https://github.com/webpack-contrib/worker-loader/blob/next/src/index.js#L40

I found that the code changed from

const filename = loaderUtils.interpolateName(this, options.name || '[hash].worker.js', {
  context: options.context || this.options.context,
  regExp: options.regExp,
});

to

const filename = loaderUtils.interpolateName(this, options.name || '[hash].worker.js', {
  context: this.context || options.context || this.options.context,
  regExp: options.regExp,
});

It is no problem.

@michael-ciniawsky
Copy link
Member

webpack v4.0.0 changed this.context => this.rootContext

context: options.context || this.rootContext || this.context

should work. PR welcome :)

@michael-ciniawsky
Copy link
Member

Fixed by #128 , released in v1.1.1 🎉

@michael-ciniawsky michael-ciniawsky removed this from the 1.1.2 milestone Feb 25, 2018
odama626 pushed a commit to odama626/service-worker-loader that referenced this issue Mar 15, 2018
timneutkens pushed a commit to vercel/next.js that referenced this issue Aug 7, 2018
## Context

When upgrading to Next.js 6.1.1-canary.4 and using the `emit-file-loader` included in Next.js, the following error is thrown:

```bash
 error  in ./src/graveyard/pages/_app.scss
Module build failed (from ../node_modules/next/dist/build/webpack/loaders/emit-file-loader.js):
TypeError: Cannot read property 'context' of undefined
    at Object.module.exports (~/project-root/node_modules/next/dist/build/webpack/load
ers/emit-file-loader.js:27:68)

 @ ./src/pages/_app.js 35:0-53 156:17-26 157:13-22
 @ multi ./pages/_app.js
```

`next.config.js` (shortened):
```js
module.exports = {
  webpack: (config, { dev }) => {
    config.module.rules.push({
      test: /\.scss$/,
      use: [
        {
          loader: 'emit-file-loader',
          options: {
            name: 'dist/[path][name].[ext].js'
          }
        },
        {
          loader: 'babel-loader',
          options: {
            babelrc: false,
            extends: path.resolve(__dirname, './src/.babelrc')
          }
        },
        'styled-jsx-css-loader',
        { loader: 'postcss-loader', options: { sourceMap: dev } },
        {
          loader: 'sass-loader',
          options: {
            sourceMap: dev
          }
        }
      ]
    });

    return config;
  }
};
```

## Suggested Fix

A quick Google search brought me to a [related issue in `webpack-loader`](webpack-contrib/worker-loader#125). As pointed out in the [Webpack docs](https://webpack.js.org/api/loaders/#this-rootcontext):

> Starting with webpack 4, the formerly `this.options.context` is provided as `this.rootContext`.

This PR makes this change while maintaining backward compatibility.

## System information

Next.js: 6.1.1-canary.4
Node: v9.3.0
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants