reactjs 如何让我的React应用默认在Chrome浏览器中打开?

mwngjboj  于 2023-06-22  发布在  React
关注(0)|答案(8)|浏览(156)

我正在使用create-react-app工具来开发React应用程序。问题是,当我使用“npm start”命令运行应用程序时,它将在Firefox浏览器中打开,我希望它默认在Chrome浏览器中打开。
我发现许多答案提供了相同的解决方案,即修改package.json文件中脚本部分的“start”,但我用不同的代码反复尝试,结果没有工作。我不知道为什么我读到的所有答案都没有解决我的问题。我正在使用Windows 7。
这是我的package.json代码:

{
  "name": "jsx",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.1"
  },
  "scripts": {
   "start": "cross-env PORT=8080 BROWSER='Chrome' react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

如果我更新package.json中脚本部分的“start”,像这样:

"start": "BROWSER='Chrome' react-scripts start"

它不会工作,我得到这个错误:

  • “BROWSER”不被识别为内部或外部命令*

这是日志文件:

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files (x86)\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files 
  (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'start' ]
2 info using npm@6.7.0
3 info using node@v11.11.0
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle jsx@0.1.0~prestart: jsx@0.1.0
6 info lifecycle jsx@0.1.0~start: jsx@0.1.0
7 verbose lifecycle jsx@0.1.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle jsx@0.1.0~start: PATH: C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\KA\react\jsx\node_modules\.bin;C:\Python27\;C:\Python27\Scripts;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Python36\Scripts\;C:\Python36\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Windows\System32\Windows System Resource Manager\bin;C:\Windows\idmu\common;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Java\jdk1.8.0\bin;C:\ProgramData\Anaconda2;C:\ProgramData\Anaconda2\Scripts;C:\ProgramData\Anaconda2\Library\bin;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Pandoc\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:\Users\KA\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files (x86)\nodejs\;C:\ProgramData\chocolatey\bin;;C:\Program Files (x86)\LINQPad5;C:\Ruby22\bin;C:\Python27;C:\Python27\Scripts;C:\Users\KA\AppData\Local\atom\bin;C:\Program Files\heroku\bin;C:\Python27\Scripts\pip.exe;C:\Users\KA\AppData\Roaming\npm;C:\Users\KA\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\KA\AppData\Roaming\npm;C:\Users\KA\.dotnet\tools
9 verbose lifecycle jsx@0.1.0~start: CWD: C:\Users\KA\react\jsx
10 silly lifecycle jsx@0.1.0~start: Args: [ '/d /s /c', "BROWSER='Chrome' react-scripts start" ]
11 silly lifecycle jsx@0.1.0~start: Returned: code: 1  signal: null
12 info lifecycle jsx@0.1.0~start: Failed to exec start script
13 verbose stack Error: jsx@0.1.0 start: `BROWSER='Chrome' react-scripts start`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:197:13)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:197:13)
13 verbose stack     at maybeClose (internal/child_process.js:984:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:265:5)
14 verbose pkgid jsx@0.1.0
15 verbose cwd C:\Users\KA\react\jsx
16 verbose Windows_NT 6.1.7601
17 verbose argv "C:\\Program Files (x86)\\nodejs\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
18 verbose node v11.11.0
19 verbose npm  v6.7.0
20 error code ELIFECYCLE
21 error errno 1
22 error jsx@0.1.0 start: `BROWSER='Chrome' react-scripts start`
22 error Exit status 1
23 error Failed at the jsx@0.1.0 start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
nlejzf6q

nlejzf6q1#

如果你的Windows

BROWSER=chrome npm start

Linux是

BROWSER=google-chrome npm start

马科斯群岛

BROWSER=google chrome npm start
kt06eoxx

kt06eoxx2#

这里你在一行中使用了两个命令,其中一个是设置环境变量,另一个是启动node.js。命令行中的命令应如下:

set BROWSER=chrome
npm start

或者你可以设置BROWSER env。系统envs中的变量,在运行node.js之前

5kgi1eie

5kgi1eie3#

将默认浏览器更改为Chrome浏览器或用途:

"start": "cross-env BROWSER='Google Chrome' react-scripts start",
jobtbby3

jobtbby34#

转到您的windows设置,搜索默认浏览器,并根据您的选择进行设置。。我也遇到了同样的问题,通过这样做解决了它...

4xy9mtcn

4xy9mtcn5#

在Mac上,开始脚本应该包含google chrome周围的引号:

"start": "BROWSER='google chrome' react-scripts start"

现在npm start会在Chrome中打开它。
如果你想灵活地使用不同的浏览器,请在package.json文件中添加以下内容:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "chrome": "BROWSER='google chrome' react-scripts start",
    "safari": "BROWSER=safari react-scripts start"
}

这些脚本适用于npm run chromenpm run safari

kpbpu008

kpbpu0086#

只需运行npm start,它将打开正常浏览器,如果您想在Chrome或任何其他浏览器中打开它,只需复制URL并将其粘贴到所需的浏览器中
本地主机:3000
在我的例子中,这是URL

bq8i3lrv

bq8i3lrv7#

在项目根目录中创建一个.env.development文件,并包含以下行:

BROWSER=chrome
bcs8qyzn

bcs8qyzn8#

在windows中,设置env变量:

set BROWSER=chrome

然后像正常一样执行npm start

相关问题