mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
1e601b6b8b
Added missing pnpm command for installing dependencies to ensure consistency with other documentation pages. <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Signed commits - This repository requires verified commit signatures on protected branches. - If this pull request is blocked for unsigned commits, re-sign the commits and force-push the branch. - A `Signed-off-by` line in the commit message is not enough. ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? Added the missing `pnpm` installation command to the `module-not-found` error documentation page. ### Why? To ensure consistency across the documentation, as other error pages and guides in Next.js typically include examples for npm, yarn, and pnpm. ### How? Added `- When using pnpm: pnpm add swr` to the "Possible Ways to Fix It" section in `errors/module-not-found.mdx`. Closes NEXT- Fixes # -->
147 lines
4.3 KiB
Plaintext
147 lines
4.3 KiB
Plaintext
---
|
|
title: Module Not Found
|
|
---
|
|
|
|
## Why This Error Occurred
|
|
|
|
A module not found error can occur for many different reasons:
|
|
|
|
- The module you're trying to import is not installed in your dependencies
|
|
- The module you're trying to import is in a different directory
|
|
- The module you're trying to import has a different casing
|
|
- The module you're trying to import uses Node.js specific modules, for example `dns`, outside of `getStaticProps` / `getStaticPaths` / `getServerSideProps`
|
|
|
|
## Possible Ways to Fix It
|
|
|
|
### The module you're trying to import is not installed in your dependencies
|
|
|
|
When importing a module from [npm](https://npmjs.com) this module has to be installed locally.
|
|
|
|
For example when importing the `swr` package:
|
|
|
|
```js filename="example.js"
|
|
import useSWR from 'swr'
|
|
```
|
|
|
|
The `swr` module has to be installed using a package manager.
|
|
|
|
- When using `npm`: `npm install swr`
|
|
- When using `yarn`: `yarn add swr`
|
|
- When using `pnpm`: `pnpm add swr`
|
|
|
|
### The module you're trying to import is in a different directory
|
|
|
|
Make sure that the path you're importing refers to the right directory and file.
|
|
|
|
### The module you're trying to import has a different casing
|
|
|
|
Make sure the casing of the file is correct.
|
|
|
|
Example:
|
|
|
|
```jsx filename="components/MyComponent.js"
|
|
export default function MyComponent() {
|
|
return <h1>Hello</h1>
|
|
}
|
|
```
|
|
|
|
```jsx filename="pages/index.js"
|
|
// Note how `components/MyComponent` exists but `Mycomponent` without the capital `c` is imported
|
|
import MyComponent from '../components/Mycomponent'
|
|
```
|
|
|
|
Incorrect casing will lead to build failures on case-sensitive environments like most Linux-based continuous integration and can cause issues with Fast Refresh.
|
|
|
|
### The module you're trying to import uses Node.js specific modules
|
|
|
|
`getStaticProps`, `getStaticPaths`, and `getServerSideProps` allow for using modules that can only run in the Node.js environment. This allows you to do direct database queries or reading data from Redis to name a few examples.
|
|
|
|
The tree shaking only runs on top level pages, so it can't be relied on in separate React components.
|
|
|
|
You can verify the tree shaking on [next-code-elimination.vercel.app](https://next-code-elimination.vercel.app/).
|
|
|
|
Example of correctly tree-shaken code:
|
|
|
|
```js filename="lib/redis.js"
|
|
import Redis from 'ioredis'
|
|
|
|
const redis = new Redis(process.env.REDIS_URL)
|
|
|
|
export default redis
|
|
```
|
|
|
|
```jsx filename="pages/index.js"
|
|
import redis from '../lib/redis'
|
|
|
|
export async function getStaticProps() {
|
|
const message = await redis.get('message')
|
|
return {
|
|
message,
|
|
}
|
|
}
|
|
|
|
export default function Home({ message }) {
|
|
return <h1>{message}</h1>
|
|
}
|
|
```
|
|
|
|
Example of code that would break:
|
|
|
|
```js filename="lib/redis.js"
|
|
import Redis from 'ioredis'
|
|
|
|
const redis = new Redis(process.env.REDIS_URL)
|
|
|
|
export default redis
|
|
```
|
|
|
|
```jsx filename="pages/index.js"
|
|
// Redis is a Node.js specific library that can't run in the browser
|
|
// Trying to use it in code that runs on both Node.js and the browser will result in a module not found error for modules that ioredis relies on
|
|
// If you run into such an error it's recommended to move the code to `getStaticProps` or `getServerSideProps` as those methods guarantee that the code is only run in Node.js.
|
|
import redis from '../lib/redis'
|
|
import { useEffect, useState } from 'react'
|
|
|
|
export default function Home() {
|
|
const [message, setMessage] = useState()
|
|
useEffect(() => {
|
|
redis.get('message').then((result) => {
|
|
setMessage(result)
|
|
})
|
|
}, [])
|
|
return <h1>{message}</h1>
|
|
}
|
|
```
|
|
|
|
Example of code that would break:
|
|
|
|
```js filename="lib/redis.js"
|
|
import Redis from 'ioredis'
|
|
|
|
// Modules that hold Node.js-only code can't also export React components
|
|
// Tree shaking of getStaticProps/getStaticPaths/getServerSideProps is ran only on page files
|
|
const redis = new Redis(process.env.REDIS_URL)
|
|
|
|
export function MyComponent() {
|
|
return <h1>Hello</h1>
|
|
}
|
|
|
|
export default redis
|
|
```
|
|
|
|
```jsx filename="pages/index.js"
|
|
// In practice you'll want to refactor the `MyComponent` to be a separate file so that tree shaking ensures that specific import is not included for the browser compilation
|
|
import redis, { MyComponent } from '../lib/redis'
|
|
|
|
export async function getStaticProps() {
|
|
const message = await redis.get('message')
|
|
return {
|
|
message,
|
|
}
|
|
}
|
|
|
|
export default function Home() {
|
|
return <MyComponent />
|
|
}
|
|
```
|