Javascript React模块分析失败:意外令牌(1:48)

Javascript React模块分析失败:意外令牌(1:48),javascript,reactjs,webpack,Javascript,Reactjs,Webpack,有人能帮我吗?我只需创建react应用程序,然后立即启动它。然后我犯了一个类似这样的错误。我对网页包了解不多 CMD ./src/index.js 1:48 Module parse failed: Unexpected token (1:48) File was processed with these loaders: * ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js * ./node_module

有人能帮我吗?我只需创建react应用程序,然后立即启动它。然后我犯了一个类似这样的错误。我对网页包了解不多

CMD

./src/index.js 1:48
Module parse failed: Unexpected token (1:48)
File was processed with these loaders:
 * ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
 * ./node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
> $RefreshRuntime$ = require('C:/Users/LENOVO/Mine/project-new/node_modules/react-refresh/runtime.js');
| $RefreshSetup$(module.id);
|
我只是在目录中键入
npx create react app./
,然后
npm start
就发生了这个错误。 我曾尝试制作3个react应用程序,但同样的事情发生了,我以前从未接触过这个网页

App.js

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

// This is a patch for mozilla/source-map#349 -
// internally, it uses the existence of the `fetch` global to toggle browser behaviours.
// That check, however, will break when `fetch` polyfills are used for SSR setups.
// We "reset" the polyfill here to ensure it won't interfere with source-map generation.
const originalFetch = global.fetch;
delete global.fetch;

const { SourceMapConsumer, SourceMapGenerator, SourceNode } = require('source-map');
const { Template } = require('webpack');

/**
 * Generates an identity source map from a source file.
 * @param {string} source The content of the source file.
 * @param {string} resourcePath The name of the source file.
 * @returns {import('source-map').RawSourceMap} The identity source map.
 */
function getIdentitySourceMap(source, resourcePath) {
  const sourceMap = new SourceMapGenerator();
  sourceMap.setSourceContent(resourcePath, source);

  source.split('\n').forEach((line, index) => {
    sourceMap.addMapping({
      source: resourcePath,
      original: {
        line: index + 1,
        column: 0,
      },
      generated: {
        line: index + 1,
        column: 0,
      },
    });
  });

  return sourceMap.toJSON();
}

/**
 * Gets a runtime template from provided function.
 * @param {function(): void} fn A function containing the runtime template.
 * @returns {string} The "sanitized" runtime template.
 */
function getTemplate(fn) {
  return Template.getFunctionContent(fn).trim().replace(/^ {2}/gm, '');
}

const RefreshSetupRuntime = getTemplate(require('./RefreshSetup.runtime')).replace(
  '$RefreshRuntimePath$',
  require.resolve('react-refresh/runtime').replace(/\\/g, '/')
);
const RefreshModuleRuntime = getTemplate(require('./RefreshModule.runtime'));

/**
 * A simple Webpack loader to inject react-refresh HMR code into modules.
 *
 * [Reference for Loader API](https://webpack.js.org/api/loaders/)
 * @this {import('webpack').loader.LoaderContext}
 * @param {string} source The original module source code.
 * @param {import('source-map').RawSourceMap} [inputSourceMap] The source map of the module.
 * @param {*} [meta] The loader metadata passed in.
 * @returns {void}
 */
function ReactRefreshLoader(source, inputSourceMap, meta) {
  const callback = this.async();

  /**
   * @this {import('webpack').loader.LoaderContext}
   * @param {string} source
   * @param {import('source-map').RawSourceMap} [inputSourceMap]
   * @returns {Promise<[string, import('source-map').RawSourceMap]>}
   */
  async function _loader(source, inputSourceMap) {
    if (this.sourceMap) {
      let originalSourceMap = inputSourceMap;
      if (!originalSourceMap) {
        originalSourceMap = getIdentitySourceMap(source, this.resourcePath);
      }

      const node = SourceNode.fromStringWithSourceMap(
        source,
        await new SourceMapConsumer(originalSourceMap)
      );

      node.prepend([RefreshSetupRuntime, '\n\n']);
      node.add(['\n\n', RefreshModuleRuntime]);

      const { code, map } = node.toStringWithSourceMap();
      return [code, map.toJSON()];
    } else {
      return [[RefreshSetupRuntime, source, RefreshModuleRuntime].join('\n\n'), inputSourceMap];
    }
  }

  _loader.call(this, source, inputSourceMap).then(
    ([code, map]) => {
      callback(null, code, map, meta);
    },
    (error) => {
      callback(error);
    }
  );
}

module.exports = ReactRefreshLoader;

// Restore the original value of the `fetch` global, if it exists
if (originalFetch) {
  global.fetch = originalFetch;
}

这是我的webpack.config.js

@pmmmwh/react刷新网页包插件/loader/index.js

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

// This is a patch for mozilla/source-map#349 -
// internally, it uses the existence of the `fetch` global to toggle browser behaviours.
// That check, however, will break when `fetch` polyfills are used for SSR setups.
// We "reset" the polyfill here to ensure it won't interfere with source-map generation.
const originalFetch = global.fetch;
delete global.fetch;

const { SourceMapConsumer, SourceMapGenerator, SourceNode } = require('source-map');
const { Template } = require('webpack');

/**
 * Generates an identity source map from a source file.
 * @param {string} source The content of the source file.
 * @param {string} resourcePath The name of the source file.
 * @returns {import('source-map').RawSourceMap} The identity source map.
 */
function getIdentitySourceMap(source, resourcePath) {
  const sourceMap = new SourceMapGenerator();
  sourceMap.setSourceContent(resourcePath, source);

  source.split('\n').forEach((line, index) => {
    sourceMap.addMapping({
      source: resourcePath,
      original: {
        line: index + 1,
        column: 0,
      },
      generated: {
        line: index + 1,
        column: 0,
      },
    });
  });

  return sourceMap.toJSON();
}

/**
 * Gets a runtime template from provided function.
 * @param {function(): void} fn A function containing the runtime template.
 * @returns {string} The "sanitized" runtime template.
 */
function getTemplate(fn) {
  return Template.getFunctionContent(fn).trim().replace(/^ {2}/gm, '');
}

const RefreshSetupRuntime = getTemplate(require('./RefreshSetup.runtime')).replace(
  '$RefreshRuntimePath$',
  require.resolve('react-refresh/runtime').replace(/\\/g, '/')
);
const RefreshModuleRuntime = getTemplate(require('./RefreshModule.runtime'));

/**
 * A simple Webpack loader to inject react-refresh HMR code into modules.
 *
 * [Reference for Loader API](https://webpack.js.org/api/loaders/)
 * @this {import('webpack').loader.LoaderContext}
 * @param {string} source The original module source code.
 * @param {import('source-map').RawSourceMap} [inputSourceMap] The source map of the module.
 * @param {*} [meta] The loader metadata passed in.
 * @returns {void}
 */
function ReactRefreshLoader(source, inputSourceMap, meta) {
  const callback = this.async();

  /**
   * @this {import('webpack').loader.LoaderContext}
   * @param {string} source
   * @param {import('source-map').RawSourceMap} [inputSourceMap]
   * @returns {Promise<[string, import('source-map').RawSourceMap]>}
   */
  async function _loader(source, inputSourceMap) {
    if (this.sourceMap) {
      let originalSourceMap = inputSourceMap;
      if (!originalSourceMap) {
        originalSourceMap = getIdentitySourceMap(source, this.resourcePath);
      }

      const node = SourceNode.fromStringWithSourceMap(
        source,
        await new SourceMapConsumer(originalSourceMap)
      );

      node.prepend([RefreshSetupRuntime, '\n\n']);
      node.add(['\n\n', RefreshModuleRuntime]);

      const { code, map } = node.toStringWithSourceMap();
      return [code, map.toJSON()];
    } else {
      return [[RefreshSetupRuntime, source, RefreshModuleRuntime].join('\n\n'), inputSourceMap];
    }
  }

  _loader.call(this, source, inputSourceMap).then(
    ([code, map]) => {
      callback(null, code, map, meta);
    },
    (error) => {
      callback(error);
    }
  );
}

module.exports = ReactRefreshLoader;

// Restore the original value of the `fetch` global, if it exists
if (originalFetch) {
  global.fetch = originalFetch;
}

//这是mozilla/source map的补丁#349-
//在内部,它使用“fetch”全局变量的存在来切换浏览器行为。
//然而,当“fetch”多填充用于SSR设置时,该检查将中断。
//我们在这里“重置”多边形填充,以确保它不会干扰源映射的生成。
const originalFetch=global.fetch;
删除global.fetch;
const{SourceMapConsumer,SourceMapGenerator,SourceNode}=require('source-map');
const{Template}=require('webpack');
/**
*从源文件生成标识源映射。
*@param{string}source源文件的内容。
*@param{string}resourcePath源文件的名称。
*@返回{import('source-map')。RawSourceMap}标识源映射。
*/
函数getIdentitySourceMap(源,资源路径){
const sourceMap=new SourceMapGenerator();
sourceMap.setSourceContent(resourcePath,source);
source.split('\n').forEach((行,索引)=>{
sourceMap.addMapping({
资料来源:resourcePath,
原件:{
行:索引+1,
列:0,
},
生成:{
行:索引+1,
列:0,
},
});
});
返回sourceMap.toJSON();
}
/**
*从提供的函数获取运行时模板。
*@param{function():void}fn包含运行时模板的函数。
*@返回{string}经过“消毒”的运行时模板。
*/
函数getTemplate(fn){
返回Template.getFunctionContent(fn.trim().replace(/^{2}/gm');
}
const refreshtsetupruntime=getTemplate(需要('./refreshtsetup.runtime')).replace(
“$RefreshRuntimePath$”,
require.resolve('react-refresh/runtime')。replace(/\\\/g,'/'))
);
const RefreshModuleRuntime=getTemplate(require('./RefreshModule.runtime');
/**
*一个简单的网页包加载器,用于向模块中注入react refresh HMR代码。
*
*[加载器API参考](https://webpack.js.org/api/loaders/)
*@this{import('webpack').loader.LoaderContext}
*@param{string}source原始模块源代码。
*@param{import('source-map').RawSourceMap}[inputSourceMap]模块的源映射。
*@param{*}[meta]传入的加载程序元数据。
*@returns{void}
*/
函数ReactRefreshLoader(源、inputSourceMap、元){
const callback=this.async();
/**
*@this{import('webpack').loader.LoaderContext}
*@param{string}源
*@param{import('source-map').RawSourceMap}[inputSourceMap]
*@returns{Promise}
*/
异步函数加载器(源,inputSourceMap){
if(this.sourceMap){
让originalSourceMap=inputSourceMap;
如果(!originalSourceMap){
originalSourceMap=getIdentitySourceMap(源,this.resourcePath);
}
const node=SourceNode.fromStringWithSourceMap(
来源
等待新的SourceMapConsumer(originalSourceMap)
);
node.prepend([RefreshSetupRuntime,'\n\n']);
添加(['\n\n',RefreshModuleRuntime]);
const{code,map}=node.toString WithSourceMap();
返回[code,map.toJSON()];
}否则{
返回[[RefreshSetupRuntime,source,RefreshModuleRuntime].join('\n\n'),inputSourceMap];
}
}
_调用(this,source,inputSourceMap),然后(
([code,map])=>{
回调(空、代码、映射、元);
},
(错误)=>{
回调(错误);
}
);
}
module.exports=ReactRefreshLoader;
//还原“fetch”全局的原始值(如果存在)
if(原始蚀刻){
global.fetch=originalFetch;
}
babel loader/lib/index.js


提前感谢

+创建反应应用程序的新版本
4.0.2
似乎存在问题
通过执行以下操作,可以使用前面的
4.0.1

  • 编辑
    package.json
    并将
    “react scripts”
    值更改为
    “4.0.1”
  • 运行
    npm安装
  • 运行
    npm启动

  • 我也遇到了同样的问题,我所做的就像上面的评论中提到的那样,我将package.json中的“react scripts”值更改为4.0.1,然后再次运行npm install。然后npm开始
    而且效果很好。在解决这个问题时,您是否使用了相同的方法或您采用了什么方法?

    在我的情况下,我的电脑名中有一个单引号字符
    (例如
    dummy'sPC
    ),这导致react scripts包在4.0.24.0.3版本中崩溃(但在我感兴趣的4.0.1版本中没有)

    将项目移动到更高级别的文件夹(例如,
    C:/Users/my app
    )并安装节点模块后,一切正常


    最后,我可以使用
    react脚本v4.0.3
    ,但前提是项目路径中不包含
    dummy'sPC
    字符串。

    您是如何创建react应用程序的?你能展示一下你所做的步骤吗?我刚做了npx创建了react应用程序,然后才开始它。你能提供一个最小的EEPRODUCTIVE示例:当然,很抱歉idk关于网页的很多问题与4.0.1版本有相同的问题解决了我的问题problem@LeandroCotti检查下面我的答案。我有一个问题,我的电脑的名称,其中包含一个单一的报价。如果您使用的是Windows,并且您的电脑名中只有一个引号,请尝试将项目文件夹移到该路径之外(将其置于C:/Users/下),并将该文件夹的权限设置为“完全控制”,直到“永远”