I have a project based on nextjs. Oddly enough, the HMR is not working properly for my project. Every time I make changes I have to re run the process. I have attached details of my next config and package.json:
next.config:
const withSass = require("#zeit/next-sass");
const withCSS = require("#zeit/next-css");
module.exports = withCSS(
withSass({
webpack(config, options) {
config.module.rules.push({
test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
use: {
loader: "url-loader",
options: {
limit: 100000,
},
},
});
return config;
}
})
);
package.json
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"export": "next export"
},
"dependencies": {
"#zeit/next-css": "^1.0.1",
"#zeit/next-sass": "^1.0.1",
"antd": "^3.26.8",
"chartjs": "^0.3.24",
"classnames": "^2.2.6",
"draft-js": "^0.11.4",
"isomorphic-unfetch": "^3.0.0",
"moment": "^2.24.0",
"next": "^9.2.1",
"node-sass": "^4.13.1",
"react": "16.12.0",
"react-dom": "16.12.0",
"react-helmet": "^5.2.1",
"react-markdown": "^4.3.1",
"react-mde": "^8.1.0",
"react-redux": "^7.2.0",
"react-select": "^3.0.8",
"react-slick": "^0.25.2",
"react-toastify": "^5.5.0",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "^2.3.0",
"showdown": "^1.9.1",
"slick-carousel": "^1.8.1"
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-loader": "^3.0.3",
"eslint-plugin-react": "^7.18.3",
"url-loader": "^3.0.0"
}
I have tried removing node_modules and reinstalling again as well, doesnt seem to fix the issue.
Following is my project structure
Got help from #felixmosh. There was issue because of my folders were case was not matching route case. Fixed the issues by changing folder name to route names.
I just solved this problem by deleting folder ".next" and then closing terminal, and run npm run dev again.
Sometimes, when you accidentally name a component starting with a lowercase and then later changes to capital letter, next cache will still consider the older name and won't count it as a normal component.
The easiest solution is to copy the contents of the components, delete the file, and create one with a proper component name.
in my case hot reload didn't work because there was assetsPrefix in my next.config.js. You can remove or change them to "/" in the development mode and everything will be as expected.
For people NOT using modules and using Typescript paths.
For my setup, I just like having general .scss files
So, in my _app.tsx
I add Styles/index.scss
Then, in my index.scss
I add my #import '~Styles/flex.scss'
take note of the ~
this was required for it to work with hot reloading.
Wrt to Next js version 10+ and in addition to Pranay's answer, make sure your routes case matches the case of the folder.
Also, I noticed the component name has to start with the upper case. If your file name is dashboard.jsx, the component name should be Dashboard.
// home/dashboard.js
const Dashboard = () => {
....
}
export default Dashboard
I had to go to package.json remove react and react-dom and re add them with yarn add.
In my case, it wasn't working on Chrome. But it was working on Edge browser. And funny enough... even on a guest chrome window.
So all I had to do was...
Clear the cookies and hard refresh.
Force audit fix worked for me. Don't know the exact reason, might be some conflicts between dependencies.
Steps
1)Run this command in your next project directory.
npm audit fix --force
you need to run npm run dev instead npm run start
Related
Im getting the above mentioned error when trying to run the npm run build for next js app. The next.js app is in typescript.
Here is the error screenshot with the folder structure
Error. Unable to understand why _app.tsx is being considered as directory here instead of a file. Any help is much appreciated. Thanks in advance!
Edit: It's just a basic next.js app created with typescript by default containing the folder structure shown in the left side in the Error screenshot above.
Below is the code inside _app.tsx:
import 'tailwindcss/tailwind.css'
import React from 'react'
import { Layout } from '../components'
import '../styles/globals.scss'
import type { AppProps } from 'next/app'
function MyApp({ Component, pageProps }: AppProps) {
return (
<Layout>
<Component {...pageProps} />
</Layout>
)
}
export default MyApp
Below is my package.json
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"graphql": "^16.3.0",
"graphql-request": "^4.1.0",
"html-react-parser": "^1.4.8",
"moment": "^2.29.1",
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-multi-carousel": "^2.8.0",
"sass": "^1.49.9"
},
"devDependencies": {
"#types/node": "17.0.4",
"#types/react": "17.0.38",
"autoprefixer": "^10.4.0",
"postcss": "^8.4.5",
"prettier": "^2.5.1",
"prettier-plugin-tailwindcss": "^0.1.1",
"tailwindcss": "^3.0.7",
"typescript": "4.5.4"
}
}
I receive error when I ran npm run build as Error: EISDIR: illegal operation on a directory, readlink 'D:\study\noobinjs\pages_app.tsx'.
Just started learning next.js and this happens in the initial encounter.
Try to put the file of project in C drive not in the D.
Example => C:\Users\Mahmoud\Desktop\next-damo
Then try to run npm run build.
This problem may be related to whether the symbolic link can be created or not.
I also encountered the same problem, but solved it by changing the filesystem from exFAT to NTFS.
As you know exFAT can't create symlink.
Overview
I use the gatsby environment.
Since graphql is used in gatsby, useStaticQuery is used.
It works fine in the development environment(gatsby develop), In the environment after building(gatsby build && gatsby serve), an error like the title has occurred.
Error Text
Error: The result of this StaticQuery could not be fetched.
This is likely a bug in Gatsby and if refreshing the page does not fix it, please open an issue in https://github.com/gatsbyjs/gatsby/issues
Error code
const data = useStaticQuery(
graphql`
query {
allArticle(sort: { order: DESC, fields: updatedAt___seconds }) {
edges {
node {
category
id
thumbnail
title
createdAt {
seconds
}
}
}
}
allRanking {
edges {
node {
id
category
title
thumbnail
}
}
}
}
`
)
※ Since it worked correctly in secret mode, I wonder if something is wrong with the cache.
What I tried
Delete gatsby
$ yarn remove gatsby
$ rm -rf node_modules
$ yarn install
$ gatsby build && gatsby serve
exec gatsby clean command
$ gatsby clean
change component name and file
https://github.com/gatsbyjs/gatsby/issues/24902#issuecomment-847926225
delete .cache file
my environment
OS : MacOS
package.json
"dependencies": {
"firebase": "^8.6.7",
"gatsby": "^3.6.2",
"gatsby-plugin-advanced-sitemap": "^2.0.0",
"gatsby-plugin-clarity": "^1.0.0",
"gatsby-plugin-dark-mode": "^1.1.2",
"gatsby-plugin-google-adsense": "^1.1.3",
"gatsby-plugin-google-gtag": "^3.7.1",
"gatsby-plugin-manifest": "^3.6.0",
"gatsby-plugin-offline": "^4.6.0",
"gatsby-plugin-react-helmet": "^4.6.0",
"gatsby-plugin-twitter": "^3.12.0",
"gatsby-source-filesystem": "^3.7.1",
"react": "^17.0.1",
"react-calendar-heatmap": "^1.8.1",
"react-copy-to-clipboard": "^5.0.4",
"react-dom": "^17.0.1",
"react-helmet": "^6.1.0",
"react-lazyload": "^3.2.0",
"react-markdown": "5.0.3",
"react-scroll": "^1.8.2",
"react-slick": "^0.28.1",
"react-syntax-highlighter": "^15.4.3",
"react-tooltip": "^4.2.21",
"remark-gfm": "^1.0.0",
"slick-carousel": "^1.8.1",
"typescript": "^4.3.2"
},
It's difficult to guess what's going on, as you pointed it seems related to cache issues, however, you've tried all the cache-related stuff. I'd suggest:
Remove gatsby-plugin-offline and add gatsby-plugin-remove-serviceworker since you won't be using service-workers anymore
Check the importation path. It should be:
import { useStaticQuery } from 'gatsby';
Check the SEO component naming and importation (i.e: Seo.js or SEO.js). The same applies for Layout
The site loses all functionalities after building it. In develop mode everything works fine, but when I build the website it looks like all scripts are missing. Bootstrap (Carousel DropDowns) are not responding, leflet map and background image not loading and react-multi-carousel do not work. I don't see any errors in the browser console, of course I ran gatsby clean before building. I uploaded the project to netlify. Below I am enclosing the json package:
{
"name": "Website",
"private": true,
"description": "Description",
"version": "0.1.0",
"license": "0BSD",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1"
},
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.35",
"#fortawesome/free-brands-svg-icons": "^5.15.3",
"#fortawesome/free-solid-svg-icons": "^5.15.3",
"#fortawesome/react-fontawesome": "^0.1.14",
"bootstrap": "^4.6.0",
"gatsby": "^3.2.1",
"gatsby-background-image": "^1.5.0",
"gatsby-image": "^3.2.0",
"gatsby-link": "^3.2.0",
"gatsby-plugin-image": "^1.2.0",
"gatsby-plugin-postcss": "^4.2.0",
"gatsby-plugin-react-helmet": "^4.2.0",
"gatsby-plugin-react-leaflet": "^3.0.0",
"gatsby-plugin-recaptcha": "^1.0.5",
"gatsby-plugin-robots-txt": "^1.5.5",
"gatsby-plugin-sass": "^4.2.0",
"gatsby-plugin-sharp": "^3.2.0",
"gatsby-plugin-sitemap": "^3.2.0",
"gatsby-remark-images": "^4.2.0",
"gatsby-source-filesystem": "^3.2.0",
"gatsby-transformer-remark": "^3.2.0",
"gatsby-transformer-sharp": "^3.2.0",
"gbimage-bridge": "^0.1.2",
"leaflet": "^1.7.1",
"node-sass": "^5.0.0",
"postcss": "^8.2.9",
"react": "^17.0.2",
"react-animations": "^1.0.0",
"react-bootstrap": "^1.5.2",
"react-dom": "^17.0.2",
"react-helmet": "^6.1.0",
"react-leaflet": "^3.1.0",
"react-multi-carousel": "^2.6.2",
"react-scripts": "^4.0.3",
"styled-components": "^5.2.3"
},
"devDependencies": {
"http-proxy-middleware": "^1.1.0",
"netlify-lambda": "^2.0.3",
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1"
}
}
There's not much debug in the question however, to me, it seems that you are using some dependencies outside React's scope, which may potentially block React's hydration process, which may explain the problem described. For example, using Leaflet instead of React's Leaflet or (depending on its implementation) Bootstrap instead of React's Boostrap.
You should be able to change all React-unfriendly modules to React's ones without too much effort and that should fix your problems.
Keep in mind that if your project "work in develop and breaks in build" doesn't mean that your project work or stops working, it just means that is working under certain and specific circumstances. Basically, and summarizing (to avoid extending the answer), gatsby develop uses the browser as an interpreter, where there are, among other things, global objects like window or document. However, gatsby build occurs in the Node server, where at the build time, there are not global objects because there are not even created yet, that the main reason why you may face a different behavior between both scenarios but doesn't mean that the project stopped working magically.
You can read further details in the Overview of Gatsby Build Process.
Another option, linked with blocking React's hydration, is that some component may be blocking that process because of its own behavior. Be careful when using global objects like window or document (or when importing modules that uses it), they use should be always be wrapped inside the following condition, as you can see from the docs:
When referencing window in a React component.
import * as React from "react"
// Check if window is defined (so if in the browser or in node.js).
const isBrowser = typeof window !== "undefined"
export default function MyComponent() {
let loggedIn = false
if (isBrowser) {
window.localstorage.getItem("isLoggedIn") === "true"
}
return <div>Am I logged in? {loggedIn}</div>
}
Or when requiring a module:
// Requiring a function causes an error during builds
// as the code tries to reference window
const module = require("module") // Error
// Wrap the require in check for window
if (typeof window !== `undefined`) {
const module = require("module")
}
The same approach can be done using loadable components as the docs suggests but without providing any piece of code it's impossible to guess what should be its implementation.
I solved the problem by wrapping the leaflet maps in Loadable
import React from 'react';
import Loadable from "#loadable/component"
const LoadableMap = Loadable(() => import("./map.js"))
export default LoadableMap;
I am using styled-jsx in my project and I just migrated it to a monorepo structure, and since then I have been having the following problem:
Type '{ children: string; jsx: true; }' is not assignable to type
'DetailedHTMLProps<StyleHTMLAttributes<HTMLStyleElement>, HTMLStyleElement>'.
Property 'jsx' does not exist on type
'DetailedHTMLProps<StyleHTMLAttributes<HTMLStyleElement>, HTMLStyleElement>'.ts(2322)
This appears under the jsx attribute every time I use the default tag for style-jsx:
<style jsx>
{`...`}
</style>
I found a closed issue about this subject, and according to this link, this issue can be solved if I manually add the following two lines to interface HTMLAttributes in react/index.d.ts:
jsx?: boolean;
global?: boolean;
This actually solved the problem, but I don't want to manually modify a file from node_modules.
According to this closed issue in vercel, I should be able to fix this by simply running yarn add -D #types/styled-jsx, but this didn't work.
Installing the packages using using npm instead of yarn fixed the problem too, but I don't want to change the package manager I am using. Furthermore, installing this one package with npm and the others with yarn crashed the application.
I thought this could be a hoisting problem related to styled-jsx and yarn workspaces, but adding
"private": true,
"nohoist":["**/styled-jsx","**/styled-jsx/**"]
to my root package.json and to the package.json of the project that uses styled-jsx didn't fix the problem either.
Does any one have a solution to this problem that does not involve manually modifying react/index.d.ts, changing my package manager or abandoning the monorepo structure?
The package.json of the project that uses styled-jsx:
{
"name": "with-typescript",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
"type-check": "tsc",
"lint": "eslint **/**",
"lint:fix": "eslint **/** --fix",
"test": "NODE_ENV=test jest --passWithNoTests --watch",
"test:ci": "NODE_ENV=test jest --passWithNoTests"
},
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.34",
"#fortawesome/free-solid-svg-icons": "^5.15.2",
"#fortawesome/react-fontawesome": "^0.1.14",
"#types/styled-jsx": "^2.2.8",
"axios": "^0.21.1",
"dotenv": "^8.2.0",
"firebase": "^8.2.4",
"firebase-admin": "^9.4.2",
"formidable": "^1.2.2",
"global": "^4.4.0",
"isomorphic-unfetch": "3.0.0",
"jest": "^25.2.1",
"js-cookie": "^2.2.1",
"next": "^10.0.5",
"path": "^0.12.7",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-router-dom": "^5.2.0",
"styled-components": "^5.2.1",
"styled-jsx": "^3.4.1"
},
"devDependencies": {
"#types/formidable": "^1.0.32",
"#types/js-cookie": "^2.2.6",
"#types/node": "^12.12.21",
"#types/react": "^16.9.16",
"#types/react-dom": "^16.9.4",
"#types/react-router-dom": "^5.1.7",
"#types/styled-components": "^5.1.7",
"#types/styled-jsx": "^2.2.8",
"#typescript-eslint/eslint-plugin": "^4.14.0",
"#typescript-eslint/parser": "^4.14.0",
"eslint": "^7.18.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",
"prettier": "^1.18.2",
"typescript": "3.7.3"
},
"license": "ISC"
}
The package.json of my root folder:
{
"name": "next_shsc",
"version": "1.0.0",
"main": "index.js",
"private":true,
"repository": "https://github.com/agaragon/next_shsc.git",
"author": "aragon <andregaragon#gmail.com>",
"license": "MIT",
"workspaces":{
"packages": [
"packages/*"
]
}
}
This solution fixed the issue for me (after #smeijer at GitHub):
Create a TypeScript Declaration File, named e.g. custom.d.ts:
import 'react';
declare module 'react' {
interface StyleHTMLAttributes<T> extends React.HTMLAttributes<T> {
jsx?: boolean;
global?: boolean;
}
}
Place it somewhere in the project, for example in the /src/typings folder. It should be picked up by the compiler automatically, but it may possibly depend on the config (?).
If you get eslint error complaining about the import 'react' line, put the following comment above it:
// eslint-disable-next-line react/no-typos`
Installing the #types/styled-jsx package didn't work for me - it's a stub for the types, as styled-jsx includes their own now.
Reading the docs on the styled-jsx github, there's a specific solution for this: https://github.com/vercel/styled-jsx#typescript
Adding a file at the root of my next app named styled-jsx.d.ts and putting this in it worked for me:
/// <reference types="styled-jsx" />
I am using Next 12.1.6 and I had this problem until I installed #types/styled-jsx 3.4.4.
I think it is a better approach.
Using pnpm 7 in a monorepo and Next 12.2.0 I had to install styled-jsx in my app with pnpm add styled-jsx
I am trying to leverage React-native-vecor-icons package in a project that uses Server-Side-Rendering react deployment (SSR).
React-native-vector-icons works fine in standard web deployments as well (not just in react-native on mobile). HOwever with Server-side-rendering there is a problem.
The package author, chose to publish his library into NPM repository with ES6 syntax for modules (eg import ... }.
While web-browsers rely on transply by babel to ES5, during app build stage -- this does not help with server side deployment. Because it is the server (NodeJs) that needs to be able to understand the package syntax.
This is, I believe, why I am getting this error.
If my understanding of the problem is correct, then I need to figure out a way to get Babel to transply this offending package on the fly and to feed it into node-js at run time.
This is where I need help, simply setting various stuff at .babelrc is not helping (see below). And I do not know how to make this work without making manual changes to the React-native-vector icons code (See at the end the manual changes I that I figured out to make... to test out the theory).
The error:
c:\Users\v\home\devel\mine\ltproject\mob\rnweb\aa1a\ssr-aft\node_modules\react-native-vector-icons\Ionicons.js:6
import createIconSet from './lib/create-icon-set';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:152:10)
at Module._compile (module.js:605:28)
at Object.Module._extensions..js (module.js:652:10)
at Module.load (module.js:560:32)
at tryModuleLoad (module.js:503:12)
at Function.Module._load (module.js:495:3)
at Module.require (module.js:585:17)
at require (internal/module.js:11:18)
at Object.react-native-vector-icons/Ionicons (c:\Users\v\home\devel\mine\ltproject\mob\rnweb\aa1a\ssr-aft\build\server.js:1527:18)
The piece of code from React Native Vector Icons library within node_modules, that's causing the problem (first import statement)
/**
* Ionicons icon set component.
* Usage: <Ionicons name="icon-name" size={20} color="#4F8EF7" />
*/
import createIconSet from './lib/create-icon-set';
import glyphMap from './glyphmaps/Ionicons.json';
const iconSet = createIconSet(glyphMap, 'Ionicons', 'Ionicons.ttf');
export default iconSet;
export const Button = iconSet.Button;
export const TabBarItem = iconSet.TabBarItem;
export const TabBarItemIOS = iconSet.TabBarItemIOS;
export const ToolbarAndroid = iconSet.ToolbarAndroid;
export const getImageSource = iconSet.getImageSource;
My package.json
{
"name": "aa1a",
"version": "0.0.1",
"license": "XYZ",
"private": true,
"scripts": {
"start": "razzle start",
"build": "razzle build",
"test": "razzle test --env=jsdom",
"start:prod": "NODE_ENV=production node build/server.js",
"vclient": "webpack --watch --progress"
},
"dependencies": {
"#jaredpalmer/after": "latest",
"airbnb-browser-shims": "^2.1.0",
"bootstrap": "^3.3.7",
"bundle-loader": "^0.5.6",
"compression": "^1.7.2",
"express": "^4.16.2",
"postcss-syntax": "^0.10",
"prop-types": "^15.6.1",
"pubsub-js": "^1.6",
"qs": "^6.5.2",
"razzle": "^2.0.0-alpha.12",
"react": "^16.3.2",
"react-art": "^16.3.2",
"react-bootstrap": "^0.32.1",
"react-dom": "^16.3.2",
"react-helmet": "^5.2.0",
"react-markdown": "^3.3.2",
"react-native-vector-icons": "^4.6.0",
"react-native-web": "^0.6.1",
"react-router-dom": "^4.3.0-rc.2",
"react-virtualized": "^9.19",
"react-virtualized-image-measurer": "^0.3",
"resize-observer-polyfill": "^1.5.0",
"serialize-javascript": "^1.4.0",
"tree-model": "^1.0.6",
"webpack-visualizer-plugin": "^0.1.11"
},
"devDependencies": {
"babel-plugin-react-native-web": "^0.7.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
"babel-preset-react-native": "^4.0.0",
"babel-register": "^6.26.0",
"mini-css-extract-plugin": "^0.4.0",
"node-sass": "^4.8.3",
"sass-loader": "^6.0.7",
"stylelint": "^9.2.1",
"stylelint-config-standard": "^18.2.0",
"treeify": "^1.1.0",
"webpack-bundle-analyzer": "^2.9.1"
}
}
I had also tried to tell babel-loader to transply into ES5 syntax compatible with node version ( (I am running node 9.10)). But installing babel-preset-env #1.3.3 (because that's what razzle/babel is using)). And, then, modifying my .babelrc with
{
"presets": ["razzle/babel",
"react-native",
[
"babel-preset-env",
{
"targets": {
"node": 9,
"esmodules": true
}
}
]
],
"plugins": ["react-native-web",
"transform-es2015-modules-commonjs",
"transform-class-properties"
]
}
That, unfortunately, did not help -- getting same error.
I also tried upgrading to node v10.2 and using
npm start -- --experimental-modules
... razzle start "--experimental-modules"
Did not work either.. although I am not 100% sure if razzle, in this case is passing the command line arg into node.js
As noted, above -- I figured out the manual code changes I need to make to get RN vector icons to start up under Node JS (I am using version 9.1)
Change 1) CD to node_modules/react-native-vector-icons. Copy Ionicons.js into Ionicons_org.js.
Change 2) Replace the Ionicons.js with this code (and delete the rest).
require('babel-register')({
plugins: ["transform-class-properties","react-native-web"],
presets: [ 'env' ],
// Ignore everything in node_modules except node_modules/react-native-vector-icons
ignore: /node_modules\/(?!react-native-vector-icons)/
})
// Import the rest of our application.
module.exports = require('./Ionicons_org.js')