Use of ES6 Webpack modules



  • I need to use ES6 modules for React+Flux application. As I realized, to use them, I kind of built it, and something works.

    e.g.:

    welcome.js

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

    and home.js

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

    They interact adequately, and they' what they need.

    But as soon as I try to connect any module loaded through npm:

    home.js

    import request from 'request';
    

    I'm going to start a hell on a command line like this: введите сюда описание изображения

    Tell them how to use ES6 modules correctly or I'm already swimming.

    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']
            }
        }]
    },
    

    };



  • Added webpack.config.js Parameter target value "node":

    {
        target: 'node'
    }
    

    This is the op. WebpackAbout what kind of Wednesday the gang is going to be. In default, the value of the op was found "web" and the gang is going under the browser. Unlike environment Node.jsbrowsers don't have built-in modules like fstlsnet And so on. node_modules/where they are obviously not, which is the result of the error. When the environment is switched Node.jsWebpack will automatically ignore the connections Node.js The modules and the assembly will be successful.

    https://webpack.github.io/docs/configuration.html#target




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2