A
A
Andrey Perov2017-01-11 01:30:45
JavaScript
Andrey Perov, 2017-01-11 01:30:45

Why does the module not work when building on webpack?

in the terminal we write webpack, we collect modules from server.js to dist.js. All without errors
If we run node dist.js, then it swears and writes that dist.js does not find such a module: 47
import http from 'http';
^^^^^^
SyntaxError: Unexpected token import
My package.json

{
  "name": "js",
  "version": "1.0.0",
  "description": "node learn",
  "main": "server.js",
  "dependencies": {
    "babel-loader": "^6.2.10",
    "webpack": "^1.14.0"
  },
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.21.0",
    "babel-loader": "^6.2.10",
    "babel-preset-es2015": "^6.18.0",
    "babelify": "^7.3.0",
    "browserify": "^13.3.0",
    "http": "0.0.0",
    "webpack-dev-server": "^1.16.2"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node_modules/.bin/webpack-dev-server"
  },
  "author": "",
  "license": "ISC"
}

webpack.config.js
module.exports = {
    entry: "./server.js",
    output: {
        path: __dirname,
        filename: "dist.js"
    }
};

server.js
import http from 'http';

console.log(http);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ro37A, 2017-01-11
@SnaIP

Webpack just doesn't know how to load es6 modules. Connect babel-loader.

module.exports = {
    entry: "./server.js",
    output: {
        path: __dirname,
        filename: "dist.js"
    },
    module : {
        loaders: [ { 
                test   : /.js$/,
                loader : 'babel-loader'
            }
        ]
    }
};

E
Evgeny Kozinchenko, 2017-01-11
@inerc

Or you can do it like this

{
  "presets": ["es2015", "react", "stage-0"],
  "plugins": [
    ["transform-runtime", {
      "polyfill": false,
      "regenerator": true
    }]
  ]
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question