错误在./~/node-env-file/lib/index.js找不到模块:错误:无法解析模块' fs'

时间:2017-05-14 17:58:51

标签: javascript node.js webpack

因此,当我尝试在config.js文件中需要node-env-file时,如下所示:

import Raven from 'raven-js';

const env = require('node-env-file');
env('.env');

我收到以下错误消息:

  错误在./~/node-env-file/lib/index.js找不到模块:错误:   无法解析模块' fs'在   C:\ Users \用户d0475 \文件\项目\学习 - 终极版 - graphql \ node_modules \节点ENV文件\ LIB   @ ./~/node-env-file/lib/index.js 3:9-22

我的webpack配置文件如下:

var path = require('path');
var webpack = require('webpack');

module.exports = {
  devtool: 'source-map',
  entry: [
    'webpack-hot-middleware/client',
    './client/app'
  ],
  output: {
    path: path.join(__dirname, 'public'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  module: {
    loaders: [
    // js
    {
      test: /\.js$/,
      loaders: ['babel'],
      include: path.join(__dirname, 'client')
    },
    // CSS
    { 
      test: /\.styl$/, 
      include: path.join(__dirname, 'client'),
      loader: 'style-loader!css-loader!stylus-loader'
    }
    ]
  }
};

和我的package.json文件如下:

{
  "name": "learn-redux-graphql",
  "version": "1.0.0",
  "description": ":) ",
  "scripts": {
    "build:webpack": "set NODE_ENV=production && webpack --config webpack.config.prod.js",
    "build": "npm run clean && npm run build:webpack",
    "test": "NODE_ENV=production mocha './tests/**/*.spec.js' --compilers js:babel-core/register",
    "clean": "rimraf dist",
    "start": "node devServer.js"
  },
  "repository": {
    "type": "git",
    "url": ""
  },
  "author": "Theo Mer",
  "license": "MIT",
  "homepage": "",
  "dependencies": {
    "apollo-client": "^1.0.2",
    "babel-core": "^6.5.2",
    "babel-eslint": "^6.1.2",
    "babel-loader": "^6.2.3",
    "babel-plugin-react-transform": "^2.0.0",
    "babel-plugin-transform-object-rest-spread": "^6.5.0",
    "babel-plugin-transform-react-display-name": "^6.5.0",
    "babel-polyfill": "^6.5.0",
    "babel-preset-es2015": "^6.5.0",
    "babel-preset-react": "^6.5.0",
    "css-loader": "^0.23.1",
    "eslint": "^3.4.0",
    "eslint-plugin-babel": "^3.1.0",
    "eslint-plugin-react": "^4.1.0",
    "express": "^4.13.4",
    "firebase": "^3.3.0",
    "graphql-tag": "^1.3.2",
    "graphql-tools": "^0.10.1",
    "immutability-helper": "^2.1.2",
    "lodash": "^4.17.4",
    "node-env-file": "^0.1.8",
    "raven-js": "^2.1.1",
    "react": "^0.14.7",
    "react-addons-css-transition-group": "^0.14.7",
    "react-apollo": "^1.0.0-rc.3",
    "react-dom": "^0.14.7",
    "react-redux": "^4.4.0",
    "react-router": "^2.0.0",
    "react-router-redux": "^4.0.0",
    "react-transform-catch-errors": "^1.0.2",
    "react-transform-hmr": "^1.0.2",
    "redbox-react": "^1.2.2",
    "redux": "^3.3.1",
    "redux-thunk": "^2.0.1",
    "rimraf": "^2.5.2",
    "style-loader": "^0.13.0",
    "stylus": "^0.54.5",
    "stylus-loader": "^2.3.1",
    "subscriptions-transport-ws": "^0.5.5-alpha.0",
    "webpack": "^1.12.14",
    "webpack-dev-middleware": "^1.5.1",
    "webpack-hot-middleware": "^2.7.1"
  },
  "devDependencies": {
    "babel-plugin-transform-react-jsx": "^6.23.0",
    "expect": "^1.14.0",
    "expect-jsx": "^2.3.0",
    "mocha": "^2.4.5",
    "react-addons-test-utils": "^0.14.7",
    "sw-precache-webpack-plugin": "^0.9.1"
  },
  "babel": {
    "presets": [
      "react",
      "es2015"
    ],
    "env": {
      "development": {
        "plugins": [
          [
            "transform-object-rest-spread"
          ],
          [
            "transform-react-display-name"
          ],
          [
            "react-transform",
            {
              "transforms": [
                {
                  "transform": "react-transform-hmr",
                  "imports": [
                    "react"
                  ],
                  "locals": [
                    "module"
                  ]
                },
                {
                  "transform": "react-transform-catch-errors",
                  "imports": [
                    "react",
                    "redbox-react"
                  ]
                }
              ]
            }
          ]
        ]
      },
      "production": {
        "plugins": [
          [
            "transform-object-rest-spread"
          ],
          [
            "transform-react-display-name"
          ]
        ]
      }
    }
  },
  "eslintConfig": {
    "ecmaFeatures": {
      "jsx": true,
      "modules": true
    },
    "env": {
      "browser": true,
      "node": true
    },
    "parser": "babel-eslint",
    "rules": {
      "quotes": [
        2,
        "single"
      ],
      "strict": [
        2,
        "never"
      ],
      "babel/generator-star-spacing": 1,
      "babel/new-cap": 1,
      "babel/object-shorthand": 1,
      "babel/arrow-parens": 1,
      "babel/no-await-in-loop": 1,
      "react/jsx-uses-react": 2,
      "react/jsx-uses-vars": 2,
      "react/react-in-jsx-scope": 2
    },
    "plugins": [
      "babel",
      "react"
    ]
  }
}

这是什么,我该如何解决?

2 个答案:

答案 0 :(得分:0)

您需要tell your webpack config about your node environment。将<?php // Basic example of PHP script to handle with jQuery-Tabledit plug-in. // Note that is just an example. Should take precautions such as filtering the input data. header('Content-Type: application/json'); $mysqli = mysqli('localhost', 'root', 'admin', 'ham'); if (mysqli_connect_errno()) { echo json_encode(array('mysqli' => 'Failed to connect to MySQL: ' . mysqli_connect_error())); exit; } $page= isset($_GET['p'])? $_GET['p'] : '' ; if($page=='view'){ $result=$mysqli->query("SELECT id, ticket_no, full_name, emp_id, emp_mail, member_type, member_location, request_type, asset_type, asset_number, serial_number, asset_model_number, remarks_comments, timestamp, status FROM HAMTRACKER WHERE is_flagged != '1';"); while($row= $result->fetch_assoc()){ ?> <tr> <td><?php echo $row['id'] ?></td> <td><?php echo $row['ticket_no'] ?></td> <td><?php echo $row['full_name'] ?></td> <td><?php echo $row['emp_id'] ?></td> <td><?php echo $row['emp_mail'] ?></td> <td><?php echo $row['member_type'] ?></td> <td><?php echo $row['member_location'] ?></td> <td><?php echo $row['request_type'] ?></td> <td><?php echo $row['asset_type'] ?></td> <td><?php echo $row['asset_number'] ?></td> <td><?php echo $row['serial_number'] ?></td> <td><?php echo $row['asset_model_number'] ?></td> <td><?php echo $row['remarks_comments'] ?></td> <td><?php echo $row['timestamp'] ?></td> <td><?php echo $row['status'] ?></td> </tr> <?php } }else{ $input = filter_input_array(INPUT_POST); if ($input['action'] === 'edit') { $mysqli->query("UPDATE hamtracker SET ticket_no='" . $input['tickettb'] . "', full_name='" . $input['fullnametb'] . "', emp_id='" . $input['employeeidtb'] . "' , emp_mail='" . $input['emailtb'] . "', member_type='" . $input['selectmembertypedd'] . "', member_location='" . $input['selectmemberlocationdd'] . "', request_type='" . $input['selectrequesttypedd'] . "', asset_type='" . $input['selectassettypedd'] . "', asset_number='" . $input['assetnumbertb'] . "', serial_number='" . $input['serialnumbertb'] . "', asset_model_number='" . $input['selectassetmodelnumberdd'] . "', remarks_comments='" . $input['remarkstb'] . "', timestamp= default, status='" . $input['status'] . "' WHERE id='" . $input['id'] . "'"); } else if ($input['action'] === 'delete') { $mysqli->query("UPDATE hamtracker SET is_flagged=1 WHERE id='" . $input['id'] . "'"); } else if ($input['action'] === 'restore') { $mysqli->query("UPDATE hamtracker SET is_flagged=0 WHERE id='" . $input['id'] . "'"); } mysqli_close($mysqli); echo json_encode($input); } ?> 条目添加到您的webpack配置中,并为node模块添加空值:

fs

答案 1 :(得分:0)

使用dotenv-webpack解决了这个问题。