I need to use ES6 modules for an application on React + Flux. As I understand it, in order to use them you need a Webpack, I sort of set it up, and even something works.

eg:

welcome.js

"use strict"; export default function (message) { console.log("Welcome ${message}"); }; 

and home.js

 "use strict"; import welcome from './welcome'; welcome("home"); 

adequately interact, and derive what is needed.

but as soon as I try to connect any module loaded via npm:

home.js

 import request from 'request'; 

when I try to build, hell begins on the command line like this: enter image description here

Plz tell me how to properly use the ES6 modules and then it already melts me.

package.json

 { "name": "gitter-demo-app", "version": "0.0.1", "dependencies": { "babel-runtime": "^6.9.2", "express": "~3.4.4", "jade": "~0.35.0", "passport": "~0.2.0", "passport-oauth2": "~1.1.1", "request": "~2.27.0" }, "devDependencies": { "babel-plugin-transform-runtime": "^6.9.0", "mocha": "", "nodemon": "~1.0.15", "webpack": "^1.13.1" }, "scripts": { "start": "node app.js", "pretest": "make restart-test-server", "test": "NODE_ENV=test mocha", "posttest": "make stop-test-server" } } 

and webpack.config.js

 const webpack = require('webpack'); module.exports = { entry : "./home", output : { filename : "build.js" }, watch : true , watchOptions : { aggregateTimeout : 100 }, devtool : "source-map", module : { loaders: [{ test: /\.js$/, exclude: /(node_modules|bower_components)/, loader: 'babel', query: { presets: ['es2015'], plugins: ['transform-runtime'] } }] }, }; 

    1 answer 1

    You need to add the target parameter with the value "node" to webpack.config.js :

     { target: 'node' } 

    This option tells Webpack 'about the environment under which the bundle is built. By default, the value of this option is set to "web" and the bundle is built for browsers. Unlike the Node.js environment, browsers do not have such “embedded” modules as fs , tls , net , etc. (with them you have errors), therefore when building for the browser, Webpack tries to find them in the node_modules / folder, where they are obviously not there, which is the result of an error. When switching environments to Node.js , the Webpack will automatically ignore the connections of the embedded Node.js modules and the build will succeed.

    Item in the Webpack documentation about the target parameter