pnpm: Not working with Gatsby

pnpm version: 1.23.2

Code to reproduce the issue:

npm i -g gatsby-cli
gatsby new test
cd test
rm -rf node_modules && rm package-lock.json
pnpm i
gatsby develop

Expected behavior: Gatsby runs

Actual behavior:

➜  test gatsby develop
/usr/local/bin/gatsby develop

Options:
  -h, --help     Show help                                                                                                                                     [boolean]
  --verbose      Turn on verbose output                                                                                                       [boolean] [default: false]
  -H, --host     Set host. Defaults to localhost                                                                                         [string] [default: "localhost"]
  -p, --port     Set port. Defaults to 8000                                                                                                   [string] [default: "8000"]
  -o, --open     Open the site in your browser for you.                                                                                                        [boolean]
  -v, --version  Show version number                                                                                                                           [boolean]

error There was a problem loading the local develop command. Gatsby may not be installed. Perhaps you need to run "npm install"?


  Error: Cannot find module 'request'

  - module.js:11 require
    internal/module.js:11:18

  - develop.js:229 Object.<anonymous>
    [test]/[1.9.155]/[gatsby]/dist/commands/develop.js:229:15

  - module.js:11 require
    internal/module.js:11:18

  - create-cli.js:53 resolveLocalCommand
    [lib]/[gatsby-cli]/lib/create-cli.js:53:14

  - create-cli.js:70 Object.handler
    [lib]/[gatsby-cli]/lib/create-cli.js:70:22

  - command.js:233 Object.self.runCommand
    [lib]/[gatsby-cli]/[yargs]/lib/command.js:233:22

  - yargs.js:990 Object.Yargs.self._parseArgs
    [lib]/[gatsby-cli]/[yargs]/yargs.js:990:30


Additional information:

  • node -v prints: 9.3.0
  • Windows, OS X, or Linux?: OS X

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 22 (13 by maintainers)

Most upvoted comments

It is a plugin, now. Finally got tired of having to write in Webpack/PNPM compatibility fixes in every Gatsby project I create, so I made a plugin to handle it for me: gatsby-plugin-pnpm. See also https://github.com/gatsbyjs/gatsby/issues/19316#issuecomment-582275227

Workaround

let gatsbyNodeModules = require('fs').realpathSync('node_modules/gatsby')
gatsbyNodeModules = require('path').resolve(gatsbyNodeModules, '..')

exports.onCreateWebpackConfig = ({ stage, actions }) => {

  actions.setWebpackConfig({
    resolve: {
      modules: [gatsbyNodeModules, 'node_modules'],
    },
  })
  
}

@cdfa This is now fixed in yurnalist 1.0.5

Great! I added a link to this plugin to https://github.com/pnpm/awesome-pnpm

I’m going to take a crack at getting pnpm working with gatsby v2.

I think the fix is a custom webpack externals config that resolves all node requires to project root relative paths.

I faced a similar problem with next.js which uses webpack to compile server side code.

The only way to workaround this seems to be to use: pnpm i --shamefully-flatten.


My pnpmfile resolutions are:

  // https://github.com/gatsbyjs/gatsby/issues/4656
  'gatsby': {
    dependencies: {
      'request': '^2',
      'address': '^1',
      'lodash': '^4',
    }
  },

  'gatsby-cli': {
    dependencies: {
      'regenerator-runtime': "^0.11.1",
    }
  },

  // https://github.com/geowarin/friendly-errors-webpack-plugin/issues/64
  'friendly-errors-webpack-plugin': {
    peerDependencies: {
      webpack: '*'
    }
  },

There were also some silent errors in gatsby which needed these top-level deps:

  • babel-core
  • babel-loader
  • react
  • react-dom

In pnpmfile should check for root pkg, and if gatsby is in deps there, add these automatically.

I guess an issue should be created at Gatsby and additional investigation done.

With this hook gatsby makes it to the bundling step:

module.exports = {
  hooks: {
    readPackage (pkg) {
      switch (pkg.name) {
        case 'gatsby-cli':
          Object.assign(pkg.dependencies, {
            'regenerator-runtime': "^0.11.1",
          })
          break
        case 'gatsby':
          Object.assign(pkg.dependencies, {
            address: '^1.0.3',
            'request': "^2.83.0",
          })
          break
      }
      return pkg
    }
  }
}

However, there it fails. Seems like because webpack searches for dependencies in project/node_modules instead of in the node_modules of gatsby

maybe try to remove node_modules and shrinkwrap.yaml after changes to pnpmfile.js

to get the output of console.log try to run pnpm install --reporter append-only