[npm] ‘브라우저’필드에 유효한 별칭 구성이 없습니다

나는 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. 웹 팩 설명서도 있지만 기본적으로 켜져있는 것 같습니다 : 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의 문제로 밝혀졌습니다. 끔찍한 끔찍한 오류 메시지에 대해 이야기하십시오.

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

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


답변

React 서버 측 렌더러를 작성 중이며 별도의 서버 구성을 처음부터 작성할 때도 발생할 수 있습니다. 이 오류가 표시되면 다음을 시도하십시오.

  1. “항목”값이 “컨텍스트”값과 관련하여 올바르게 경로를 지정했는지 확인하십시오. 항목 파일 이름 앞에 “./”가 없습니다.
  2. “해결”값이 포함되어 있는지 확인하십시오. node_modules의 항목에 대한 가져 오기는 기본적으로 “context”폴더를 찾는 것으로 설정됩니다.

예:

const serverConfig = {
name: 'server',
context: path.join(__dirname, 'src'),
entry: {serverEntry: ['./server-entry.js']},
output: {
    path: path.join(__dirname, 'public'),
    filename: 'server.js',
    publicPath: 'public/',
    libraryTarget: 'commonjs2'
},
module: {
    rules: [/*...*/]
},
resolveLoader: {
    modules: [
        path.join(__dirname, 'node_modules')
    ]
},
resolve: {
    modules: [
        path.join(__dirname, 'node_modules')
    ]
}
};


답변

나는 같은 문제가 있었지만 내 경로는 잘못된 케이싱 때문이었습니다.

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

// Correct - lowercase c in /pathcoordinate/
./path/pathcoordinate/pathCoordinateForm.component


답변

필자의 경우 다음 package.json과 같은 상대 경로 로 종속성으로 설치된 패키지였습니다 .

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

js/app.js함께 수입import "phoenix_html"

이것은 작동했지만 노드, npm 등의 업데이트 후 위의 오류 메시지와 함께 실패했습니다.

가져 오기 라인을 변경하여 import "../../deps/phoenix_html"수정했습니다.


답변

내 항목을로 변경

entry: path.resolve(__dirname, './src/js/index.js'),

그리고 효과가있었습니다.


답변

내 경우, 끝날까지 webpack.config.js내가해야 exports, 오타는 config가 있었다 : export(해야한다 exports), 로딩 실패로 이어졌다 webpack.config.js전혀.

const path = require('path');

const config = {
    mode: 'development',
    entry: "./lib/components/Index.js",
    output: {
        path: path.resolve(__dirname, 'public'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: path.resolve(__dirname, "node_modules")
            }
        ]
    }
}

// pay attention to "export!s!" here
module.exports = config;


답변

필자의 경험에 따르면이 오류는 Webpack에서 별칭의 이름이 잘못 지정 되었기 때문에 발생했습니다. 거기에 별명이라는 이름이 redux있었고 webpack은 redux내 별명 경로에서 redux 패키지와 함께 제공 되는 것을 찾습니다 .

이 문제를 해결하려면 별칭의 이름을와 다른 이름으로 바꿔야했습니다 Redux.