简体   繁体   English

模块解析失败:意外的令牌 - 反应

[英]Module parse failed: Unexpected token - React

i am trying to create a react component as a npm package and was following this link https://medium.com/quick-code/publish-your-own-react-component-as-npm-package-under-5-minutes-8a47f0cb92b9 and I am pretty new to this.我正在尝试将反应组件创建为 npm package 并关注此链接https://medium.com/quick-component-codes-publish-pm-your-package-under-5-component-codes-publish-pm-your-package-under-5-component-codes-minute-package -8a47f0cb92b9 ,我对此很陌生。

This is the error I am getting when I run npm.start这是我运行 npm.start 时遇到的错误

ERROR in ./src/index.js 6:4
Module parse failed: Unexpected token (6:4)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|   const { width, height, color, text } = props;
|   return (
>     <div
|       style={{
|         width: width || 100,

I looked online and seems to an issue with webpack.config but I am not sure where it is wrong in mine.我在网上查看,似乎 webpack.config 有问题,但我不确定我的哪里错了。

These are my files这些是我的文件

webpack.config webpack.config

var path = require('path');
module.exports = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'index.js',
    libraryTarget: 'commonjs2'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        include: path.resolve(__dirname, 'src'),
        exclude: /(node_modules|bower_components|build)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['env']
          }
        }
      }
    ]
  },
  externals: {
    'react': 'commonjs react' 
  }
};

.babelrc .babelrc

    {
    "presets": ["env"],
    "plugins": [
        "transform-object-rest-spread",
        "transform-react-jsx"
    ]
}

src/index.js src/index.js

import React from "react";

const ReactColorSquare = props => {
  const { width, height, color, text } = props;
  return (
    <div
      style={{
        width: width || 100,
        height: height || 100,
        backgroundColor: color || "blue"
      }}
    >
      {text}
    </div>
  );
};

export default ReactColorSquare;

package.json package.json

{
  "name": "reactnpmpackage",
  "version": "1.0.0",
  "description": "",
  "main": "build/index.js",
  "scripts": {
    "start": "webpack --watch",
    "build": "webpack",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^16.13.1",
    "webpack": "^4.43.0"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.1.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-react-jsx": "^6.24.1",
    "babel-preset-env": "^1.7.0",
    "webpack-cli": "^3.3.11"
  }
}

Here is my project structure这是我的项目结构

在此处输入图像描述

Could you please correct where I am getting it wrong..你能纠正我哪里弄错了吗..

Try it with @babel/preset-react .尝试使用@babel/preset-react Do an npm i --save-dev @babel/preset-react , then add it to your.babelrc file:执行npm i --save-dev @babel/preset-react ,然后将其添加到 your.babelrc 文件中:

// .babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    "transform-object-rest-spread",
    "transform-react-jsx"
  ]
}

You might also try changing your libraryTarget to the universal module definition:您也可以尝试将libraryTarget更改为通用模块定义:

libraryTarget: 'umd'

Or add mode: development to your module.exports in the webpack.config.或者在 webpack.config 中添加mode: development到你的module.exports

This solved a similar problem for me a few times.这几次为我解决了类似的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM