简体   繁体   English

意外的令牌'<'错误(npx create-react-app)

[英]Unexpected token '<' Error (npx create-react-app)

I've setup a new react app using npx create-react-app my-app .我已经使用 npx create-react-app my-app设置了一个新的反应应用程序。

In the index.html file I want to import a javascript file in the head like so在 index.html 文件中,我想像这样在头部导入一个 javascript 文件

<script src="/src/.../file.js"></script>

However, I keep getting Unexpected token '<' error.但是,我不断收到 Unexpected token '<' 错误。 I can't do an import as it creates loads of errors and I want to be able to use some functionality in these scripts across the site.我无法进行导入,因为它会产生大量错误,并且我希望能够在整个站点的这些脚本中使用某些功能。

Any ideas how to reference this file?任何想法如何引用这个文件? Previously in other projects we use webpack but this config doesn't seem available in this project.以前在其他项目中我们使用 webpack 但此配置在此项目中似乎不可用。

If your js files are located in the /public/ folder then you can import it like this:如果您的 js 文件位于 /public/ 文件夹中,那么您可以像这样导入它:

<script src="%PUBLIC_URL%/.../file.js"></script>

If it is located in /src/ folder, you can simply import it to your index.js file, and it will be available across your React application.如果它位于 /src/ 文件夹中,您只需将其导入您的index.js文件,它将在您的 React 应用程序中可用。 Remember that order of imports matters here.请记住,进口顺序在这里很重要。

import './.../file.js';

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

相关问题 语法错误:意外令牌,应为{(1:7),create-react-app - Syntax error: Unexpected token, expected { (1:7) , create-react-app 错误:在 npx create-react-app 运行期间产生 UNKNOWN - Error: spawn UNKNOWN during npx create-react-app run 运行 npx create-react-app 时遇到错误 - Facing error while running npx create-react-app npx create-react-app 安装时显示错误 - npx create-react-app show error when installing 使用 Flask &quot;Uncaught SyntaxError: Unexpected token &lt;&quot; 为 create-react-app 提供服务 - Serving a create-react-app with Flask "Uncaught SyntaxError: Unexpected token <" “编译失败”“解析错误:意外的令牌:”在尝试迁移到现有 create-react-app 中的 typescript 期间 - "Failed to Compile" "Parsing error: Unexpected token :" during attempt to migrate to typescript in existing create-react-app npx create-react-app<app name> 什么都不做 没有错误 没有安装</app> - npx create-react-app <app name> doing nothing no error no installation npx create-react-app my-app 错误:rollbackFailedOptional - npx create-react-app my-app ERORR: rollbackFailedOptional 使用 npx create-react-app 创建的应用无法启动, - App created with npx create-react-app failed to start, npx create-react-app 致命错误:JavaScript 堆内存不足 - npx create-react-app fatal error: JavaScript heap out of memory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM