获取ReferenceError:无法找到变量:require react packpack?

时间:2016-10-26 07:17:46

标签: javascript node.js express reactjs nodemailer

使用gmail-send(https://www.npmjs.com/package/gmail-send#code-example)nom模块从font-end创建电子邮件发送选项。在实施此问题时,我发现了这个问题' ReferenceError:无法找到变量:require'。我是否错过了web-pack-config.js?

这是我的webpack config.js代码,

var webpack = require('webpack');
var LiveReloadPlugin = require('webpack-livereload-plugin');

    module.exports = {
        target: 'node',
        entry:['./src/custom-script.js', './src/main.js'],
        output: {
          path: __dirname,
          filename: "build/bundle.js"
        },
        resolve: {
          extensions: ['', '.js', '.jsx', '.json', 'index.json']
        },
        module: {
          loaders: [
            {test: /\.json$/, loader: "json-loader"},
            {
              test: /\.jsx?$/,
              loader: ['babel-loader'],
              exclude: /node_modules/,
              query: {
                presets: ['react', 'es2015', 'stage-0']
              }
            }
          ]
        },
        plugins: [
          new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
          }),
          new webpack.optimize.DedupePlugin(),
          new webpack.optimize.OccurenceOrderPlugin(),
          new LiveReloadPlugin()
        ]
    };

反应代码,

import React from 'react';
import { render } from 'react-dom';

class SendMail extends React.Component {
    handleSubmit() {
        var send = require('gmail-send')({
          user: 'user@gmail.com',               // Your GMail account used to send emails 
          pass: 'abcdefghijklmnop',             // Application-specific password 
          to:   '"User" <user@gmail.com>',      // Send back to yourself 
          // from:   '"User" <user@gmail.com>'  // from: by default equals to user 
          // replyTo:'user@gmail.com'           // replyTo: by default undefined 
          subject: 'test subject',
          text:    'test text'
          // html:    '<b>html text text</b>' 
        });

        send({                         
          subject: 'attached '
        }, function (err, res) {
          console.log('send(): err:', err, '; res:', res);
        });
    }
    render() {
        return (
            <div>
                <button className="button button-primary button-fluid" onClick={this.handleSubmit.bind(this)}>Send code</button>
            </div>
        );
    }
}

export default SendMail;

我在这段代码中错过了任何内容!!

1 个答案:

答案 0 :(得分:0)

为什么不将gmailSend导入为 -

import React from 'react';
import { render } from 'react-dom';
import gmailSend from 'gmail-send'; // Importing

class SendMail extends React.Component {
    handleSubmit() {
        var send = gmailSend({ // calling
                ...
        });
    }
       ...
}

export default SendMail;