加载依赖项

时间:2017-06-29 15:10:02

标签: webpack package.json

文件:

的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的依赖关系。

我会在理解从哪里开始解决这个问题时感谢一些帮助:)。

1 个答案:

答案 0 :(得分:0)

事实证明,我超级天真。此simple-oauth2库仅适用于node.js,无法在浏览器中运行。

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

某些node.js库的浏览器版本已作为request

等软件包提供