【问题标题】:Reactjs Unexpected token Error Only On Windows, Not LinuxReactjs 意外令牌错误仅在 Windows 上,而不是 Linux
【发布时间】:2018-07-31 12:29:49
【问题描述】:

所以在我的第一个 ReactJS 教程中,我使用的是 Ubuntu VM,并记得遇到这个问题是因为我忘记安装 react 和 react-dom 依赖项。我现在在 Windows 上,确保安装了所有东西,我得到了同样的错误:

对于我的 package.json,我有这个:

{
  "name": "github-battle",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "dependencies": {},
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "css-loader": "^0.28.9",
    "html-webpack-plugin": "^2.30.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "style-loader": "^0.20.2",
    "webpack": "^3.11.0",
    "webpack-dev-server": "^2.11.1"
  },
  "scripts": {
    "create": "webpack"
  },
  "author": "",
  "license": "ISC"
}

对于我的 webpack.config.js

var path = require('path');
 var HtmlWebpackPlugin = require('html-webpack-plugin');

 module.exports = {
   entry: './app/index.js',
   output: {
     path: path.resolve(__dirname, 'dist'),
     filename: 'index_bundle.js',
     publicPath: '/'
   },
   module: {
     rules: [
       { test: /\.(js)$/, use: 'babel-loader' },
       { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]}
     ]
   },
   devServer: {
     historyApiFallback: true,
    },
   plugins: [
     new HtmlWebpackPlugin({
       template: 'app/index.html'
     })
   ]
 };

index.js

var React = require('react');
var ReactDOM = require('react-dom');
require('./index.css');

class App extends React.Component{
    render(){
        return(
            <div>Hello World</div>
        )
    }
}

ReactDOM.render(<App />, document.getElementById('app'));

在 Windows 中有什么我必须做的不同的事情吗?

【问题讨论】:

  • 您应该仔细检查换行符类型。您应该在 Windows 中更改回 CR LR,在 Linux 中更改为 LF。使用任何代码编辑器(例如 Notepad++)来更改它。
  • 问题是我做了一个全新的项目,我没有从我的 Linux VM 复制任何东西
  • 不。即使您在 Windows 中创建文件,仍然有可能换行模式不正确。
  • 我使用的是 Notepad++,它说行尾格式是 Windows,编码是 UTF-8

标签: javascript html json reactjs webpack


【解决方案1】:

我尝试使用您粘贴的所有文件创建一个新项目,但我遇到了同样的错误。我通过运行这两个命令解决了它:

npm install --save react  
npm install --save react-dom

我还添加了一个包含以下内容的 .babelrc 文件:

{
  "presets": ["react"]
}

【讨论】:

  • 这给了我更多错误。似乎它不识别选项。它说“在没有加载程序的情况下提供的错误选项/查询(用户加载程序 + {CODE YOU GAVE ME HERE} 中的选项)”
  • 我尝试了新的答案,但它抛出了完全相同的错误
  • 版本都在上面的 package.json 中列出。我没有 .babelrc 文件。我从来没有在 linux 上安装过,所以 windows 和 linux 之间的设置可能有点不同
  • webpack 是 5.6.0。我会在上面发布 index.js
  • 更新的 NPM 和使用该预设时的新消息是“./node_modules/lodash/lodash.js 中的错误。模块构建失败:错误:找不到预设 @babel/preset-env 相对于目录 C:\\ReactJS\\github-battle\\node_modules\\lodash"
猜你喜欢
  • 2018-09-10
  • 2014-01-21
  • 2019-02-19
  • 2017-10-07
  • 1970-01-01
  • 2019-05-26
  • 2016-05-03
  • 2018-04-17
  • 2016-06-15
相关资源
最近更新 更多