我已经开始使用webpack2(准确地说,v2.3.2),在重新创建我的配置后,我一直遇到一个问题,我似乎无法解决我得到(提前为丑陋的转储道歉):

ERROR in ./src/main.js
Module not found: Error: Can't resolve 'components/DoISuportIt' in '[absolute path to my repo]/src'
resolve 'components/DoISuportIt' in '[absolute path to my repo]/src'
  Parsed request is a module
  using description file: [absolute path to my repo]/package.json (relative path: ./src)
    Field 'browser' doesn't contain a valid alias configuration
    aliased with mapping 'components': '[absolute path to my repo]/src/components' to '[absolute path to my repo]/src/components/DoISuportIt'
      using description file: [absolute path to my repo]/package.json (relative path: ./src)
        Field 'browser' doesn't contain a valid alias configuration
      after using description file: [absolute path to my repo]/package.json (relative path: ./src)
        using description file: [absolute path to my repo]/package.json (relative path: ./src/components/DoISuportIt)
          as directory
            [absolute path to my repo]/src/components/DoISuportIt doesn't exist
          no extension
            Field 'browser' doesn't contain a valid alias configuration
            [absolute path to my repo]/src/components/DoISuportIt doesn't exist
          .js
            Field 'browser' doesn't contain a valid alias configuration
            [absolute path to my repo]/src/components/DoISuportIt.js doesn't exist
          .jsx
            Field 'browser' doesn't contain a valid alias configuration
            [absolute path to my repo]/src/components/DoISuportIt.jsx doesn't exist
[[absolute path to my repo]/src/components/DoISuportIt]
[[absolute path to my repo]/src/components/DoISuportIt]
[[absolute path to my repo]/src/components/DoISuportIt.js]
[[absolute path to my repo]/src/components/DoISuportIt.jsx]

package.json

{
  "version": "1.0.0",
  "main": "./src/main.js",
  "scripts": {
    "build": "webpack --progress --display-error-details"
  },
  "devDependencies": {
    ...
  },
  "dependencies": {
    ...
  }
}

关于它所抱怨的浏览器字段,我能够找到的文档是:package-browser-field-spec。也有webpack文档,但它似乎默认开启:aliasFields: ["browser"]。我尝试在我的包中添加一个浏览器字段。Json,但这似乎没有任何好处。

webpack.config.js

import path from 'path';
const source = path.resolve(__dirname, 'src');

export default {
  context: __dirname,
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js',
  },
  resolve: {
    alias: {
      components: path.resolve(__dirname, 'src/components'),
    },
    extensions: ['.js', '.jsx'],
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        include: source,
        use: {
          loader: 'babel-loader',
          query: {
            cacheDirectory: true,
          },
        },
      },
      {
        test: /\.css$/,
        include: source,
        use: [
          { loader: 'style-loader' },
          {
            loader: 'css-loader',
            query: {
              importLoader: 1,
              localIdentName: '[path]___[name]__[local]___[hash:base64:5]',
              modules: true,
            },
          },
        ],
      },
    ],
  },
};

src / main.js

import DoISuportIt from 'components/DoISuportIt';

src /组件/ DoISuportIt / index.jsx

export default function() { ... }

为完整起见,.babelrc

{
  "presets": [
    "latest",
    "react"
  ],
  "plugins": [
    "react-css-modules"
  ],
  "env": {
    "production": {
      "compact": true,
      "comments": false,
      "minified": true
    }
  },
  "sourceMaps": true
}

我做错了什么/遗漏了什么?


当前回答

就我而言,

我错误地从package.json中删除了一个库(“mini-create-react-context”)。我把它加了回来,并安装和构建了应用程序,它开始正常工作。所以请看看你的包裹。Json文件一次。

其他回答

我的情况与@witheng的回答类似。

在某个时候,我注意到在我的开发环境中的一些文件名中有一些大小写错误。例如,文件名为

type.ts

我把它改名为

Type.ts

在我的Mac开发环境中,这没有注册为git中的更改,所以这个更改没有进入源代码控制。

在基于linux的构建机器中,文件名是区分大小写的,它无法找到具有不同大小写的文件。

为了避免将来出现类似的问题,我在repo中运行了以下命令:

git config core.ignorecase false

在我的情况下,我导入库文件如下:

import { MyFile } from "my-library/public-api";

在我从导入中删除了public-api之后,一切都正常工作:

import { MyFile } from "my-library";

MyFile在库中的public-api文件中导出。

我有tsconfig.json的别名:

{
    "compilerOptions": {
        "paths": {
            "@store/*": ["./src/store/*"]
        }
    },
}

所以我通过给webpack添加别名来解决这个问题。还配置:

module.exports = {
  //...
  resolve: {
    alias: {
      '@store': path.resolve(__dirname, '../src/store'),
    },
  },
};

对我来说,问题是我在进口

.ts files into .js files

将它们改为ts也解决了这个问题。

我在一个TypeScript项目中遇到了这个错误。在我的webpack.config.js文件中,我只解析TypeScript文件。

resolve: {
    extensions: [".ts"],
}

然而,我注意到导致错误的node_module:

字段“browser”不包含有效的别名配置

没有”。ts”文件(这是可以理解的,因为模块已经转换为香草JS。哎!)。

所以为了解决这个问题,我更新了resolve声明:

resolve: {
    extensions: [".ts", ".js"],
}