【问题标题】:Webpack error when loading dependencies of a dependency加载依赖项的依赖项时出现 Webpack 错误
【发布时间】:2017-12-03 09:04:55
【问题描述】:

文件:

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 版本。 如果不是加载simple-oauth2,而是加载lodash,它可以工作。 lodash 没有依赖关系,所以我最好的猜测是我当前的 webpack 配置无法处理 simple-oauth2 的依赖关系。

在理解从哪里开始解决这个问题时,我将不胜感激:)。

【问题讨论】:

    标签: webpack package.json


    【解决方案1】:

    事实证明,我太天真了。这个simple-oauth2 库仅适用于 node.js,它不能像在浏览器中那样运行。

    这就是http://browserify.org/ 的来源,例如:http://www.bradoncode.com/tutorials/browserify-tutorial-node-js/

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

    【讨论】:

      猜你喜欢
      • 2012-11-18
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      • 2018-01-02
      • 1970-01-01
      相关资源
      最近更新 更多