配置对象无效 - Webpack

时间:2018-05-01 21:48:45

标签: node.js webpack

我对Webpack很新,​​并尝试在项目中使用Twitter API。在我需要twitter依赖项和配置文件之前,一切正常。然后我收到以下错误消息:

Can't resolve 'fs'
Can't resolve 'net'
Can't resolve 'tls'

这是我的package.json

{
  "name": "webpack-4-setup",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack --mode development",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.6.1",
    "webpack": "^4.6.0",
    "webpack-cli": "^2.1.2"
  },
  "dependencies": {
    "twitter": "^1.7.1"
  }
}

config.js

module.exports = {
   consumer_key: '',
   consumer_secret: '',
   access_token_key: '',
   access_token_secret: ''
}

index.js

const Twitter = require('twitter');
const config = require('./config.js');

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  }
};

我已经找到了答案,却找不到任何解释错误的答案。我在添加

时找到了答案
node: {
    net: "empty",
    tls: "empty"
}
webpack.config中的

,但是又出现了另一个错误:Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.

有人可以解释一下我做错了什么以及这里发生了什么?我会非常感激的!

项目文件结构:

project
-dist
---main.js
-index.html
-node_modules
-package-lock.json
-package.json
-src
---config.js
---index.js
-webpack.config.js

0 个答案:

没有答案