简体   繁体   English

模块构建失败:未知词(2:9)| bundle / webpack的问题(react,redux,js,node)

[英]Module build failed: Unknown word (2:9) | Problem with bundle/webpack (react, redux, js, node)

Problem: 问题:

I've looked everywhere for someone with a similar problem and had a busy person also kindly spend some time trying to help me, but I'm not finding docs or other resources very helpful, has anyone else had this issue? 我到处寻找有类似问题的人,并且有一个忙碌的人也请花一些时间来帮助我,但是我发现文档或其他资源不是很有帮助,其他人有这个问题吗?

This is whats happened so far: 到目前为止,这是什么情况:

I firstly get the error whenI was trying to import a css style like so, 当我尝试像这样导入css样式时,我首先收到错误消息,

import 'react-day-picker/lib/style.css'

the error being: 错误是:

ERROR in ./node_modules/react-day-picker/lib/style.css Module parse
     failed: Unexpected token (3:0) You may need an appropriate loader 
     to handle this file type. | /* DayPicker styles */ | | .DayPicker { |  
     display: inline-block; |   font-size: 1rem;  @
     ./client/components/Component.jsx 21:0-41  @
     ./client/components/App.jsx  @ ./client/index.js

So I have added a css-loader and updated my webpack as well to try fix this issue but I still get: 所以我添加了一个css-loader并更新了我的webpack来尝试解决此问题,但是我仍然得到:

ERROR in ./node_modules/css-loader!./client/index.js
Module build failed: Unknown word (2:9)

 1 | import React from 'react'
> 2 | import {render} from 'react-dom'
    |         ^
  3 | import {Provider} from 'react-redux'
  4 | import {createStore, applyMiddleware, compose} from 'redux'
  5 | import thunkMiddleware from 'redux-thunk'

 @ ./client/index.js 3:14-73

Please Help! 请帮忙! Below are my files. 以下是我的文件。


webpack.config.js webpack.config.js

const path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: path.join(__dirname, '/index.js'),
  output: {
    path: path.join(__dirname, '../server/public'),
    filename: 'bundle.js'
  },
  module: {
      rules: [
      {
        test: /\.jsx?$/,
        loader: ['babel-loader', 'style-loader', 'css-loader' ],
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify(process.env.NODE_ENV)
      }
    })
  ],
  devtool: 'source-map'
}

package.json package.json

"dependencies": {
    "@babel/core": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-react": "^7.0.0",
    "babel-preset-stage-0": "^6.24.1",
    "@babel/register": "^7.0.0",
    "babel-loader": "^8.0.0",
    "body-parser": "^1.17.2",
    "cookie-parser": "^1.4.3",
    "css-loader": "^1.0.0",
    "dotenv": "^6.0.0",
    "express": "^4.16.3",
    "express-jwt": "^5.3.1",
    "jsonwebtoken": "^8.3.0",
    "jwt-decode": "^2.2.0",
    "knex": "^0.14.4",
    "leaflet": "^1.3.1",
    "passport": "^0.4.0",
    "pg": "^7.4.3",
    "prop-types": "^15.6.1",
    "react": "^16.1.1",
    "react-day-picker": "^7.2.2",
    "react-dom": "^16.1.1",
    "react-geocode": "^0.1.2",
    "react-leaflet": "^1.9.1",
    "react-redux": "^5.0.6",
    "react-router-dom": "^4.1.2",
    "redux": "^3.7.2",
    "redux-thunk": "^2.1.0",
    "sodium": "^2.0.3",
    "style-loader": "^0.23.0",
    "superagent": "^3.8.1",
    "webpack": "^4.17.1",
    "webpack-command": "^0.4.1"

  },
  "devDependencies": {
    "enzyme": "^3.2.0",
    "enzyme-adapter-react-16": "^1.1.0",
    "jest": "^21.1.0",
    "jsdom": "^11.2.0",
    "nodemon": "^1.11.0",
    "redux-mock-store": "^1.5.3",
    "sqlite3": "^3.1.9",
    "supertest": "^3.0.0",
    "webpack-dev-server": "^3.1.7"
  }
}

Solution: 解:

webpack.config.js webpack.config.js

const path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: path.join(__dirname, '/index.js'),
  output: {
    path: path.join(__dirname, '../server/public'),
    filename: 'bundle.js'
  },
  module: {
      rules: [
      {
        test: /\.jsx?$/,
        loader: ['babel-loader'],
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: [ 'style-loader', 'css-loader' ]
    }
    ]
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify(process.env.NODE_ENV)
      }
    })
  ],
  devtool: 'source-map'
}

Your css file loader needs to be separate from jsx file loader. 您的css文件加载器需要与jsx​​文件加载器分开。

Resolve loader to import a css file in your config: 解决加载程序以在配置中导入css文件:

    module.exports = {
      module: {
         rules: [
          {
              test: /\.css$/,
              use: [ 'style-loader', 'css-loader' ]
          }
         ]
        }
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Webpack构建中包括Redux节点模块 - Include Redux Node Module In Webpack Build 节点 JS Webpack 构建失败 - Node JS Webpack Build Failed 模块构建失败(来自 ./node_modules/postcss-loader/src/index.js):SyntaxError (2:14) 未知词 - Module build failed (from ./node_modules/postcss-loader/src/index.js): SyntaxError (2:14) Unknown word 模块构建失败 - Webpack、React、Babel - Module Build Failed - Webpack, React, Babel Webpack SyntaxError - 模块构建失败(来自./node_modules/babel-loader/lib/index.js) - Webpack SyntaxError - Module build failed (from ./node_modules/babel-loader/lib/index.js) Webpack模块构建失败的意外令牌(rails反应构建) - Webpack module build failed unexpected token (rails react build) 错误:模块构建失败(来自./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js): - Error: Module build failed (from ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js): 模块构建失败(来自./node_modules/@nuxt/webpack/node_modules/babel-loader/lib/index.js): - Module build failed (from ./node_modules/@nuxt/webpack/node_modules/babel-loader/lib/index.js): 与Redux反应:模块构建失败:SyntaxError:意外令牌 - React with Redux: Module build failed: SyntaxError: Unexpected token < 使用Webpack4和Babel的React应用程序模块构建失败 - Module Build Failed for react application using Webpack4 and Babel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM