babel / babel-loader

📦 Babel loader for webpack
MIT License
4.83k stars 451 forks source link

webpack 2 crashes on "modules": false with no information #362

Closed goldylucks closed 7 years ago

goldylucks commented 7 years ago

Delete what isn't applicable below I'm submitting a bug report

Webpack Version: 2.x

Babel Core Version: 6.x

Babel Loader Version: 6.2.x

Please tell us about your environment: Linux, Ubuntu 16.04

Current behavior: ERROR in TypeError: __webpack_require__(...) is not a function

  - index.ejs:1205 Object.../node_modules/webpack/buildin/harmony-module.js
    /home/adam/apps/memory-n-back/web/index.ejs:1205:1

  - index.ejs:622 __webpack_require__
    /home/adam/apps/memory-n-back/web/index.ejs:622:30

  - index.ejs:46 fn
    /home/adam/apps/memory-n-back/web/index.ejs:46:20

  - index.ejs:1197 Object.../node_modules/webpack/buildin/global.js
    /home/adam/apps/memory-n-back/web/index.ejs:1197:1

  - index.ejs:622 __webpack_require__
    /home/adam/apps/memory-n-back/web/index.ejs:622:30

  - index.ejs:46 fn
    /home/adam/apps/memory-n-back/web/index.ejs:46:20

  - index.ejs:1189 Object.../node_modules/lodash/lodash.js
    /home/adam/apps/memory-n-back/web/index.ejs:1189:1

Expected/desired behavior: not crashing, and working with webpack 2 tree shaking :)

checking out this commit from my repo and running npm start

The thing is I don't know what is causing the crash, because as u can c above, the logs are not very informational, hence I'm opening this issue and not asking on SO

webpack:2.1.0-beta.25 webpack-dev-server: 2.1.0-beta.0

it crashes on both webpack and dev server

hiroppy commented 7 years ago

Could you confirm with webpack@2.2.0?

joshwiens commented 7 years ago

http://stackoverflow.com/questions/33683539/webpack-require-is-not-a-function-when-using-babel-6

In particular, the comment about Babel returning an Object when using require() with Babel 6.x

joshwiens commented 7 years ago

That said, with the number of fixes between beta.25 & 2.2.0, I would encourage you get get out of the beta releases sooner than later. Lots of Perf love in there as well.

joshwiens commented 7 years ago

npm start on webpack 2.2.0

screen shot 2017-01-23 at 9 14 21 pm

goldylucks commented 7 years ago

@abouthiroppy I'm still getting the same error on webpack@2.2.

@d3viant0ne did u change modules to false? I read the SO thread before submitting this issue. Removing the requires didn't resolve the issue

any other ideas?

Really wanna use tree shaking :)

joshwiens commented 7 years ago

I'll have some free time this afternoon, I'll dig around a bit and see what I can find.

giannif commented 7 years ago

When using .babelrc and trying to specify modules:false, I get an immediate error on import being undefined, so I'm assuming the preset isn't taking at all.

.babelrc

"presets": [
    ["latest", {
        "es2015": {
            "modules": false
        }
    }], "stage-0", "react"
],

However, configuring the babel-loader in my wepback config works:

const jsLoader = {
    test: /.js?$/,
    use: [{
        loader: 'babel-loader',
        options: {
            "babelrc": false,
            "presets": [
                ["latest", {
                    "es2015": {
                        "modules": false
                    }
                }], "stage-0", "react"
            ],

Also, specifying "babelrc": false, reduces my bundles size. Lost on that one, since .bablerc includes the same configuration as my webpack config.

Using: webpack: 2.2.1 babel-loader: 6.2.10

loganfsmyth commented 7 years ago

Please include the full error message.

giannif commented 7 years ago

@loganfsmyth

/path-to-project/webpack.config.babel.js:1
(function (exports, require, module, __filename, __dirname) { import _extends from "babel-runtime/helpers/extends";
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:78:16)
    at Module._compile (module.js:543:28)
    at loader (/path-to-project/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/path-to-project/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at requireConfig (/path-to-project/node_modules/webpack/bin/convert-argv.js:96:18)
loganfsmyth commented 7 years ago

That error is from ES6 in your Webpack config, not from babel-loader. The issue is that your .babelrc says not to compile modules, but webpack.config.babel.js runs in Node, not in the browser, so it does need modules compiled.

giannif commented 7 years ago

@loganfsmyth makes perfect sense! thanks for clarifying