收到 "module not found" 和 "field 'browser' doesn't contain a valid alias configuration" 将我的站点部署到 Netlify

Receiving a "module not found" and "field 'browser' doesn't contain a valid alias configuration" deploying my site to Netlify

我的 GatsbyJS 站点在本地运行良好,但在部署到 Netlify 时无法生成成功的构建。我已经研究了我收到的错误并且没有任何运气。更改文件名大小写或更改文件路径不起作用。

Link to repository

确保 netlify 知道构建应用程序所需的节点版本。 https://www.netlify.com/docs/build-settings/#build-environment-variables

还要确保您的项目能够从头开始构建。将存储库克隆到一个新目录并尝试构建它。

在您的本地计算机上,运行 命令 gatsby build 将导致您在图像中显示的错误。

您会注意到错误行:

Error: ./src/components/header.js
... Can't resolve 'components/variables.css' in ...

opt/build/repo/node_modules/components/variables.css doesn't exist

告诉您它正在尝试将 components/variables.css 解析为您项目中的一个模块。

解决方案

header.js 中更改 variables.css 的导入行:

src/components/header.js

import styled from 'styled-components'
import 'components/variables.css'
...

以下内容:

src/components/header.js

import styled from 'styled-components'
import './variables.css'
...