Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should not assume package.json is available at CWD #7161

Closed
1 of 2 tasks
wangjiaojiao77 opened this issue Apr 12, 2022 · 7 comments · Fixed by #7187
Closed
1 of 2 tasks

Should not assume package.json is available at CWD #7161

wangjiaojiao77 opened this issue Apr 12, 2022 · 7 comments · Fixed by #7187
Labels
bug An error in the Docusaurus core causing instability or issues with its execution

Comments

@wangjiaojiao77
Copy link

wangjiaojiao77 commented Apr 12, 2022

Have you read the Contributing Guidelines on issues?

Description

I put everything related to docusaurus in the docs folder and level it with package.json, there was no problem before, but recently there was an error.

everything related to the document is in the docs folder, the docs directory structure as follows:

docs      
├── babel.config.js
├── build
│   └── copyTranslations.js
├── docs
│   └── getting-started.md
├── docusaurus.config.js
├── i18n
│   └── zh-cn
│       ├── code.json
│       ├── docusaurus-plugin-content-blog
│       │   └── options.json
│       ├── docusaurus-plugin-content-docs
│       │   ├── current
│       │   └── current.json
│       └── docusaurus-theme-classic
│           ├── footer.json
│           └── navbar.json
├── sidebars.js
├── src
│   ├── css
│   │   └── custom.css
│   ├── pages
│   │   ├── index.js
│   │   └── index.module.css
│   └── theme
│       └── ReactLiveScope
│           └── index.js
└── static
    └── img
        └── .gitkeep

package.json

{
  "scripts": {
    "docs": "cd docs && docusaurus",
    "docs:dev": "cd docs && docusaurus start",
    "docs:build": "cd docs && docusaurus build",
    "docs:swizzle": "cd docs && docusaurus swizzle",
    "docs:deploy": "cd docs && docusaurus deploy",
    "docs:clear": "cd docs && docusaurus clear",
    "docs:serve": "cd docs && docusaurus serve",
    "docs:write-translations": "cd docs && docusaurus write-translations",
    "docs:write-heading-ids": "cd docs && docusaurus write-heading-ids",
    "docs:copy-translations": "node build/generateExportFile.js"
  },
  "devDependencies": {
    "@docusaurus/core": "2.0.0-beta.15",
    "@docusaurus/preset-classic": "2.0.0-beta.15",
    "@docusaurus/theme-live-codeblock": "^2.0.0-beta.15",
    "docusaurus-plugin-module-alias": "0.0.2",
    "@mdx-js/react": "^1.6.21",
    "clsx": "^1.1.1",
    "prism-react-renderer": "^1.2.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "webpack": "^5.61.0",
    "webpack-cli": "^4.3.0"
  }
}

then running the npm run docs:build and other docusaurus-related commands to get an error:

internal/modules/cjs/loader.js:905
  throw err;
  ^

Error: Cannot find module '/Users/jojo/oker/blade/docs/package.json'
Require stack:
- /Users/jojo/oker/blade/node_modules/@docusaurus/core/bin/beforeCli.js
- /Users/jojo/oker/blade/node_modules/@docusaurus/core/bin/docusaurus.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.<anonymous> (/Users/jojo/oker/blade/node_modules/@docusaurus/core/bin/beforeCli.js:84:19)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.<anonymous> (/Users/jojo/oker/blade/node_modules/@docusaurus/core/bin/docusaurus.js:26:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/jojo/oker/blade/node_modules/@docusaurus/core/bin/beforeCli.js',
    '/Users/jojo/oker/blade/node_modules/@docusaurus/core/bin/docusaurus.js'
  ]
}

Self-service

  • I'd be willing to address this documentation request myself.
@wangjiaojiao77 wangjiaojiao77 added documentation The issue is related to the documentation of Docusaurus status: needs triage This issue has not been triaged by maintainers labels Apr 12, 2022
@jt1010

This comment was marked as spam.

@Josh-Cena
Copy link
Collaborator

Josh-Cena commented Apr 12, 2022

This has been brought up in the past, but I didn't find is severe enough for an immediate fix. The problem is indeed that we assume the package.json file exists at the root of CWD (and site dir, which defaults to CWD, but I can't remember clearly, it's all a bit messy). We have multiple places where we make that assumption.

I plan to do a minor refactor regarding importing various JSON modules (mostly package.json) and I'll take a look at this.

For the time being, please prefer using the siteDir argument instead of using cd docs—it's almost always a bad idea to change CWD in the middle of an npm script.

yarn start docs

@Josh-Cena Josh-Cena added bug An error in the Docusaurus core causing instability or issues with its execution and removed documentation The issue is related to the documentation of Docusaurus status: needs triage This issue has not been triaged by maintainers labels Apr 12, 2022
@Josh-Cena Josh-Cena changed the title Does the docs folder have to be at the same level as the package.json file? Should not assume package.json is available at CWD Apr 12, 2022
@wangjiaojiao77
Copy link
Author

This has been brought up in the past, but I didn't find is severe enough for an immediate fix. The problem is indeed that we assume the package.json file exists at the root of CWD (and site dir, which defaults to CWD, but I can't remember clearly, it's all a bit messy). We have multiple places where we make that assumption.

I plan to do a minor refactor regarding importing various JSON modules (mostly package.json) and I'll take a look at this.

For the time being, please prefer using the siteDir argument instead of using cd docs—it's almost always a bad idea to change CWD in the middle of an npm script.

yarn start docs

it's ok, thanks!

@fiveLucky
Copy link

This is a good issue.

@Josh-Cena Josh-Cena added closed: question This issue is a user error/misunderstanding. and removed bug An error in the Docusaurus core causing instability or issues with its execution labels Apr 12, 2022
@Josh-Cena Josh-Cena changed the title Should not assume package.json is available at CWD Does the docs folder have to be at the same level as the package.json file? Apr 12, 2022
@Josh-Cena Josh-Cena changed the title Does the docs folder have to be at the same level as the package.json file? Should not assume package.json is available at CWD Apr 12, 2022
@Josh-Cena Josh-Cena added bug An error in the Docusaurus core causing instability or issues with its execution and removed closed: question This issue is a user error/misunderstanding. labels Apr 12, 2022
@Josh-Cena
Copy link
Collaborator

Actually, let's re-open. I strongly suspect we have made some naïve assumptions about folder structure that can probably be made more robust, I just haven't really looked through everything yet

@Josh-Cena Josh-Cena reopened this Apr 12, 2022
@slorber
Copy link
Collaborator

slorber commented Apr 13, 2022

Your tree structure doesn't show where package.json is. I'm assuming it's in the parent folder of site root.

I put everything related to docusaurus in the docs folder and level it with package.json

Why do you want this to be supported exactly? this is not the typical fs structure for a Docusaurus site

Technically we could look for a package.json in all parent folders, similar to nodejs resolution algorithm, but it's important to understand your motivation to have this folder structure in the first place.

@Josh-Cena
Copy link
Collaborator

@slorber This is the case where a user has a "monorepo-like" FS structure but does not have multiple package.json files. I've seen this multiple times, and very often they would use cd in their npm script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants