字段“浏览器”不包含有效的别名配置

我已经开始使用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": {
    ...
  }
}

browser其抱怨领域而言,我能够找到的文档是:package-browser-field-spec也有webpack文档,但默认情况下似乎已将其打开:aliasFields: ["browser"]我尝试向我的browser字段添加内容package.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
}

我在做什么错/想念?

猿神奇梅2020/03/23 09:41:42

在我的情况下,我的webpack.config.js文件底部没有导出。只需添加

export default Config;

解决了。

老丝2020/03/23 09:41:42

对于所有使用Ionic的用户:更新到最新的@ ionic / app-scripts版本会提供更好的错误消息。

npm install @ionic/app-scripts@latest --save-dev

对于组件中的styleUrls到不存在的文件,这是错误的路径。奇怪的是,它在开发中没有出错。

2020/03/23 09:41:42

以我的经验,此错误是由于Webpack中别名的命名不正确造成的。因为我有一个别名redux,webpack尝试redux在别名路径中查找redux软件包随附的。

要解决此问题,我必须将别名重命名为,如Redux

泡芙2020/03/23 09:41:42

对于任何构建离子应用程序并尝试上传的人。确保您至少向该应用程序添加了一个平台。否则,您将收到此错误。

蛋蛋飞云2020/03/23 09:41:41

我遇到了同样的问题,但是我的原因是路径中的错误大小写:

// Wrong - uppercase C in /pathCoordinate/
./path/pathCoordinate/pathCoordinateForm.component

// Correct - lowercase c in /pathcoordinate/
./path/pathcoordinate/pathCoordinateForm.component
DavaidTony宝儿2020/03/23 09:41:41

在我的情况下,这是一个作为依赖项安装的软件包,package.json具有如下相对路径:

"dependencies": {
  ...
  "phoenix_html": "file:../deps/phoenix_html"
},

js/app.jsimport "phoenix_html"

这已经奏效,但是在更新节点,npm等之后,它因上述错误消息而失败。

更改导入线以import "../../deps/phoenix_html"对其进行修复。