npm - 字段 'browser' 不包含有效的别名配置

标签 npm webpack commonjs webpack-2 package.json

我已经开始使用 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"] 。我尝试将 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/components/DoISuportIt/index.jsx

export default function() { ... }

为了完整起见,.babelrc

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

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

最佳答案

结果是 Webpack 的一个问题,只是没有解决导入问题 - 谈论可怕的错误消息:(

// I Had to change:
import DoISuportIt from 'components/DoISuportIt';

// to (notice the missing `./`)
import DoISuportIt from './components/DoISuportIt';

关于npm - 字段 'browser' 不包含有效的别名配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43037590/

相关文章:

module - 闭包编译器是否支持 CommonJS 风格的 require 和 js 文件?

webpack - Vue.js 2 Webpack 3 如何从 CDN 将外部 .js 插入到 webpack 外部?

node.js - npm 启动 webpack 问题?

node.js - 使用导入时,TypeScript 将无法解析模块

javascript - 如何为nodeJs项目创建模板原型(prototype)

javascript - 使用 webpack 嵌入 HTML 文件

Javascript CommonJS - 模块在哪里?

node.js - 如何在链接的 npm 依赖项(开发中)和已安装的依赖项(暂存/生产中)之间切换?

node.js - 在Linux/macOS上安装Electronic时出现问题

gradle - 如何在Gradle构建中运行自定义任务?