jchip / optional-require

NodeJS Require that let you handle module not found error without try/catch
9 stars 5 forks source link

Getting "Can't resolve 'module'" error on Next.js project #11

Open Dentrax opened 2 years ago

Dentrax commented 2 years ago
$ npm install --save optional-require
import { optionalRequire } from "optional-require";

const foo = optionalRequire("bar.json");

Any finally:

error - ./node_modules/require-at/create-require.js:3:0
Module not found: Can't resolve 'module'

Import trace for requested module:
./node_modules/require-at/require-at.js
./node_modules/optional-require/dist/index.js
./node_modules/optional-require/index.js
Node v17.4.0

Any ideas on this? @jchip 🤔

jchip commented 2 years ago

this is for working with the native node.js require only. In next.js it's using webpack to compile and bundle .js for the browser, so it wouldn't work there.

inspiraller commented 1 year ago

I have just followed these instructions to set up a nextjs project and I get the same error.

1. Have vscode open with terminal gitbash open - pointing at the c:\apps 2. Running create nextjs project from cmder.

steps (From cmder): 2.1 npx create-next-app@latest projectname --typescript

Need to install the following packages: create-next-app Ok to proceed? (y) y √ Would you like to use ESLint with this project? ... No / Yes √ Would you like to use src/ directory with this project? ... No / Yes √ Would you like to use experimental app/ directory with this project? ... No / Yes √ What import alias would you like configured? ... @/*

  1. cd projectname
  2. npm run dev

same error Import trace for requested module: ./node_modules/next/dist/shared/lib/router/utils/is-bot.js ./node_modules/next/dist/shared/lib/router/router.js ./node_modules/next/dist/client/router.js ./node_modules/next/dist/client/dev/on-demand-entries-client.js

./node_modules/next/dist/shared/lib/router/utils/is-dynamic.js There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. Use equal casing. Compare these module identifiers:

I found the solution: First when creating the project with npm run dev I need to do it from vscode I think there is something in nextjs which recognises the a temporary directory from vscode has been opened and it expects the command coming from that.