加载依赖项的依赖项时 webpack 出错

Webpack error when loading dependencies of a dependency

文件:

package.json

{
  "main": "./src/index.js",
  "scripts": {
    "start": "webpack --display-error-details --config ./webpack-config.js"
  },
  "devDependencies": {
    "webpack": "^3.0.0"
  },
  "dependencies": {
    "simple-oauth2": "^1.2.0"
  }
}

webpack-config.js

const path = require('path');

module.exports = {
  entry: path.resolve(__dirname, 'src/index.js'),
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};

index.js

var simpleOauth2 = require('simple-oauth2');

当我运行npm start时,我得到:

ERROR in ./node_modules/joi/lib/string.js
Module not found: Error: Can't resolve 'net' in '/home/someone/github/nickname/webpack-demo/node_modules/joi/lib'
resolve 'net' in '/home/someone/github/nickname/webpack-demo/node_modules/joi/lib'
  Parsed request is a module
  using description file: /home/someone/github/nickname/webpack-demo/node_modules/joi/package.json (relative path: ./lib)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: /home/someone/github/nickname/webpack-demo/node_modules/joi/package.json (relative path: ./lib)
    resolve as module
      /home/someone/github/nickname/webpack-demo/node_modules/joi/lib/node_modules doesn't exist or is not a directory
      /home/someone/github/nickname/webpack-demo/node_modules/joi/node_modules doesn't exist or is not a directory
      /home/someone/github/nickname/webpack-demo/node_modules/node_modules doesn't exist or is not a directory
      /home/someone/github/nickname/node_modules doesn't exist or is not a directory
      /home/someone/github/node_modules doesn't exist or is not a directory
      /home/someone/node_modules doesn't exist or is not a directory
      /home/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
      looking for modules in /home/someone/github/nickname/webpack-demo/node_modules
        using description file: /home/someone/github/nickname/webpack-demo/package.json (relative path: ./node_modules)
          Field 'browser' doesn't contain a valid alias configuration
        after using description file: /home/someone/github/nickname/webpack-demo/package.json (relative path: ./node_modules)
          using description file: /home/someone/github/nickname/webpack-demo/package.json (relative path: ./node_modules/net)
            as directory
              /home/someone/github/nickname/webpack-demo/node_modules/net doesn't exist
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /home/someone/github/nickname/webpack-demo/node_modules/net doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /home/someone/github/nickname/webpack-demo/node_modules/net.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /home/someone/github/nickname/webpack-demo/node_modules/net.json doesn't exist
[/home/someone/github/nickname/webpack-demo/node_modules/joi/lib/node_modules]
[/home/someone/github/nickname/webpack-demo/node_modules/joi/node_modules]
[/home/someone/github/nickname/webpack-demo/node_modules/node_modules]
[/home/someone/github/nickname/node_modules]
[/home/someone/github/node_modules]
[/home/someone/node_modules]
[/home/node_modules]
[/node_modules]
[/home/someone/github/nickname/webpack-demo/node_modules/net]
[/home/someone/github/nickname/webpack-demo/node_modules/net]
[/home/someone/github/nickname/webpack-demo/node_modules/net.js]
[/home/someone/github/nickname/webpack-demo/node_modules/net.json]
 @ ./node_modules/joi/lib/string.js 5:12-26
 @ ./node_modules/joi/lib/index.js
 @ ./node_modules/simple-oauth2/index.js
 @ ./src/index.js

我读过关于在 webpack 中解析模块的内容,我尝试了几个 webpack 版本。 如果我加载 lodash 而不是加载 simple-oauth2,它就可以工作。 lodash 因为没有依赖关系,所以我最好的猜测是我当前的 webpack 配置无法处理 simple-oauth2.

的依赖关系

如果能帮助我理解从哪里开始解决这个问题 :)。

原来,我超级天真。此 simple-oauth2 库仅适用于 node.js,不能像在浏览器中那样 运行。

这里是http://browserify.org/ comes in, example here: http://www.bradoncode.com/tutorials/browserify-tutorial-node-js/

一些 node.js 库的浏览器版本已经作为软件包提供,例如 request: