如何通过webpack加载bugsense.js?

时间:2016-04-22 23:15:23

标签: webpack webpack-dev-server

到目前为止,我的webpack dev服务器可以正常使用我现有的一组依赖项。

现在我想在我的项目中使用bugsensebugsense库不在npm存储库中。

这是我在package.json

中包含它的方式
   "dependencies": {
     "bootstrap": "^3.3.6",
     "bugsense": "bugsense/bugsense.js",   
     ...

该软件包已按预期从github下载。

然而,当我尝试在es6 js脚本中导入它时,就像这样

import bugsense from 'bugsense';

我收到了这些错误消息

Module not found: Error: Cannot resolve module 'bugsense' in /Users/antkong/dev/projects/web/app/scripts/actions
resolve module bugsense in /Users/antkong/dev/projects/web/app/scripts/actions
  looking for modules in /Users/antkong/dev/projects/webview/node_modules
    /Users/antkong/dev/projects/webview/node_modules/bugsense doesn't exist (module as directory)
    resolve 'file' bugsense in /Users/antkong/dev/projects/webview/node_modules
      resolve file
        /Users/antkong/dev/projects/webview/node_modules/bugsense doesn't exist
        /Users/antkong/dev/projects/webview/node_modules/bugsense.webpack.js doesn't exist
        /Users/antkong/dev/projects/webview/node_modules/bugsense.web.js doesn't exist
        /Users/antkong/dev/projects/webview/node_modules/bugsense.js doesn't exist
        /Users/antkong/dev/projects/webview/node_modules/bugsense.json doesn't exist
  looking for modules in /Users/antkong/dev/projects/web/node_modules
    resolve 'file' bugsense in /Users/antkong/dev/projects/web/node_modules
      resolve file
        /Users/antkong/dev/projects/web/node_modules/bugsense is not a file
        /Users/antkong/dev/projects/web/node_modules/bugsense.webpack.js doesn't exist
        /Users/antkong/dev/projects/web/node_modules/bugsense.web.js doesn't exist
        /Users/antkong/dev/projects/web/node_modules/bugsense.js doesn't exist
        /Users/antkong/dev/projects/web/node_modules/bugsense.json doesn't exist
    resolve 'file' or 'directory' /Users/antkong/dev/projects/web/node_modules/bugsense
      resolve file
        /Users/antkong/dev/projects/web/node_modules/bugsense is not a file
        /Users/antkong/dev/projects/web/node_modules/bugsense.webpack.js doesn't exist
        /Users/antkong/dev/projects/web/node_modules/bugsense.web.js doesn't exist
        /Users/antkong/dev/projects/web/node_modules/bugsense.js doesn't exist
        /Users/antkong/dev/projects/web/node_modules/bugsense.json doesn't exist
      resolve directory
        directory default file index
          resolve file index in /Users/antkong/dev/projects/web/node_modules/bugsense
            /Users/antkong/dev/projects/web/node_modules/bugsense/index doesn't exist
            /Users/antkong/dev/projects/web/node_modules/bugsense/index.webpack.js doesn't exist
            /Users/antkong/dev/projects/web/node_modules/bugsense/index.web.js doesn't exist
            /Users/antkong/dev/projects/web/node_modules/bugsense/index.js doesn't exist
            /Users/antkong/dev/projects/web/node_modules/bugsense/index.json doesn't exist
        use ./lib/node/index.js from package.json
          resolve 'file' or 'directory' ./lib/node/index.js in /Users/antkong/dev/projects/web/node_modules/bugsense
            resolve file
              /Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js.webpack.js doesn't exist
              /Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js doesn't exist
              /Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js.web.js doesn't exist
              /Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js.js doesn't exist
              /Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js.json doesn't exist
            resolve directory
              /Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js doesn't exist (directory default file)
              /Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js/package.json doesn't exist (directory description file)
[/Users/antkong/dev/projects/web/node_modules/bugsense.webpack.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense.webpack.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense.web.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense.web.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense.json]
[/Users/antkong/dev/projects/web/node_modules/bugsense.json]
[/Users/antkong/dev/projects/webview/node_modules/bugsense]
[/Users/antkong/dev/projects/webview/node_modules/bugsense]
[/Users/antkong/dev/projects/webview/node_modules/bugsense.webpack.js]
[/Users/antkong/dev/projects/webview/node_modules/bugsense.web.js]
[/Users/antkong/dev/projects/webview/node_modules/bugsense.js]
[/Users/antkong/dev/projects/webview/node_modules/bugsense.json]
[/Users/antkong/dev/projects/web/node_modules/bugsense/index]
[/Users/antkong/dev/projects/web/node_modules/bugsense/index.webpack.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense/index.web.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense/index.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense/index.json]
[/Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js.webpack.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js.web.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js.js]
[/Users/antkong/dev/projects/web/node_modules/bugsense/lib/node/index.js.json]
 @ ./app/scripts/actions/someActions.js 21:16-35

由于文件丢失,Webpack无法加载模块。

我该如何解决?我可以通过一些webpack配置简单地修复它吗?

1 个答案:

答案 0 :(得分:1)

问题是bugsense没有正确设置 package.json main字段。 Webpack基于此确定了获取库的位置。

要解决此问题,您可以像这样定义resolve.alias

{
  resolve: {
    alias: {
      bugsense: path.join(__dirname, 'bugsense/bugsense.js/lib/amd/bugsense.js')
    }
  }
}

您可能需要尝试该路径。

您还可以考虑指向缩小版本,然后设置module.noParse,以便webpack知道避免解析文件。这可能会带来一些额外的表现。