webpack icon indicating copy to clipboard operation
webpack copied to clipboard

Problems with pnpm and the way loaders are resolved

Open donaldpipowitch opened this issue 9 years ago • 49 comments

Do you want to request a feature or report a bug?

bug?

What is the current behavior?

I can't create libs which use webpack and loaders, install these libs with pnpm and try to use them, because the modules can't be found.

It looks like pnpm installs my lib here: /Users/foo/test/ws-pnpm-test/node_modules/.registry.npmjs.org/@mercateo/ws/1.0.2/node_modules/@mercateo/ws.

The loader is here /Users/foo/test/ws-pnpm-test/node_modules/.registry.npmjs.org/string-replace-loader and here /Users/foo/test/ws-pnpm-test/node_modules/.registry.npmjs.org/@mercateo/ws/1.0.2/node_modules/string-replace-loader.

I think webpack only looks here for loaders: /Users/foo/test/ws-pnpm-test/node_modules/string-replace-loader, but this does not exist.

I think webpack resolves loaders in node_modules relative to process.cwd(), but not relative to my lib.

I can't just rewrite my config to something like this:

{
  resolveLoader: {
    modules: [
       // goes up '@mercateo/ws/dist' to match `'/Users/foo/test/ws-pnpm-test/node_modules/.registry.npmjs.org/@mercateo/ws/1.0.2/node_modules/string-replace-loader'`
       // this could lead to different directories, if my lib isn't installed with pnpm
       join(__dirname, '..', '..', '..'),
       // this would match `'/Users/foo/test/ws-pnpm-test/node_modules/.registry.npmjs.org/string-replace-loader'`
       // but I can't be sure, that my users use the official registry and not a private mirror or something like that
       join(process.cwd(), 'node_modules/.registry.npmjs.org'),
      'node_modules'  // default
    ]
  }
}

What is the most robust way to solve this? Can I specify something like that? resolveLoaders.basedir: __dirname, so all loaders can be resolved relative to my lib?

// cc @zkochan

If the current behavior is a bug, please provide the steps to reproduce.

package.json:

{
  "name": "ws-pnpm-test",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "ws": "ws"
  },
  "ws": {
    "type": "browser"
  },
  "dependencies": {
    "@mercateo/ws": "^1.0.2"
  }
}

tsconfig.json:

{}

src/index.ts:

console.log('hello');
$ pnpm install
$ npm run -s ws -- build
ERROR in Entry module not found: Error: Can't resolve 'string-replace-loader' in '/Users/foo/test/ws-pnpm-test'

What is the expected behavior?

Build without error.

If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

Node LTS, Webpack 2.6, pnpm 0.71, Mac OS X.

donaldpipowitch avatar Jun 19 '17 06:06 donaldpipowitch

Maybe I can just use resolveLoaders.modules: [ join(__dirname, '..', '..', '..'), 'node_modules' ]?

If my lib is installed with pnpm the path matches '/Users/foo/test/ws-pnpm-test/node_modules/.registry.npmjs.org/@mercateo/ws/1.0.2/node_modules. For npm and yarn it would match '/Users/foo/test/ws-pnpm-test/node_modules'. Looks okay I guess?

Feel free to close this issue, if this is the intended way.

donaldpipowitch avatar Jun 19 '17 07:06 donaldpipowitch

cc @yegor-sytnyk, @max-devjs

Ref https://github.com/pnpm/pnpm/issues/801

We have issues in lots of applications because webpack can't resolve packages in the node_modules created by pnpm. I think it is because webpack doesn't resolve dependencies the way Node does. Node resolves requires from the real path of a module. That is why the symlink approach pnpm uses works with all Node apps. However, it seems to confuse webpack and some other tools like browserify and eslint.

zkochan avatar Jun 19 '17 07:06 zkochan

Also, I believe @andreypopp has looked into these issues in scope of CRA https://github.com/pnpm/pnpm/issues/581

And @vjpr has investigated it a bit

zkochan avatar Jun 19 '17 07:06 zkochan

This issue had no activity for at least half a year.

It's subject to automatic issue closing if there is no activity in the next 15 days.

webpack-bot avatar Dec 25 '17 23:12 webpack-bot

bump

andreineculau avatar Dec 26 '17 23:12 andreineculau

Problem still exists? Can you create minimum reproducible test repo?

alexander-akait avatar Jun 06 '18 10:06 alexander-akait

I think I'm hitting this issue or something similar in this React project:

https://github.com/dirtprotocol/dirt/tree/master/sampleprojects/sample-web-react

If I install with npm or yarn, everything is fine when I run npm start. If I install with pnpm, I get this error:

/Users/sendrecv/sproj/dirt/packages/lib/dist/services/StaticContractProvider.js
Module not found: Can't resolve '@dirt/contracts' in '/Users/sendrecv/sproj/dirt/packages/lib/dist/services'

markerdmann avatar Feb 04 '19 23:02 markerdmann

i also get a similar issue with pnpm

this webpack.config.js works when i have no node_modules and run npm install however it does not work when i have no node_modules and run pnpm install

const HtmlWebPackPlugin = require("html-webpack-plugin")
const path = require("path")

const htmlWebpackPlugin = new HtmlWebPackPlugin(
{
  template: "./public/index.html",
  filename: "index.html",
})

module.exports =
{
  entry: ["./src/index.js"],
  devServer: {
    historyApiFallback: true,
    overlay: true,
  },
  resolve: {
    modules: [
      path.resolve(__dirname, "node_modules"),
      path.resolve(__dirname, "src")
    ],
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
      },
      {
        test: /\.css$/,
        use: [
          { loader: "style-loader" },
          {
            loader: "css-loader",
            options:
            {
              modules: true,
              importLoaders: 1,
              localIdentName: "[name]_[local]_[hash:base64]",              
            },
          },
        ],
      },
    ],
  },
  plugins: [htmlWebpackPlugin],
}

also my package.json

{
  "name": "mydashboard",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "webpack-dev-server --mode development --hot --port 3000",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.3.4",
    "@babel/preset-env": "^7.3.4",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.5",
    "css-loader": "^2.1.0",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.29.5",
    "webpack-cli": "^3.2.3",
    "webpack-dev-server": "^3.2.1"
  },
  "dependencies": {
    "react": "^16.8.3",
    "react-dom": "^16.8.3"
  }
}

mariotee avatar Feb 28 '19 02:02 mariotee

@sokra any ideas? This issue is still prevalent and old. pnpm is very promising and this is blocker.

nmccready avatar Apr 16 '19 22:04 nmccready

webpack should have a peeDependency to events, because hot module loading requires it

https://github.com/webpack/webpack/blob/81cf088cd6a0231b94fa2399bd29294eccee1907/hot/emitter.js#L1

viceice avatar Apr 30 '19 12:04 viceice

failed in pnpm 3.8, too

NikitaIT avatar Sep 13 '19 10:09 NikitaIT

/cc @sokra i think we need add to peeDependency optional dep events

alexander-akait avatar Sep 13 '19 10:09 alexander-akait

@NikitaIT what is problem?

alexander-akait avatar Sep 13 '19 10:09 alexander-akait

@evilebottnawi

  1. it all started with this error https://github.com/vuejs/vue-cli/issues/4571
  2. i update pnpm to 3.8.1 and solve it
  3. after build I found an error Module not found: Error: Can't resolve 'ts-loader' and same errors for tslib and @types/node.
  4. after forse install tslib and @types/node in package.json my app started
  5. but failed in runtime with Module not found: Error: Can't resolve 'ts-loader'
  6. after install ts-loader same as in vue-cli deps, i found Module build failed (from ../common/temp/node_modules/.registry.npmjs.org/babel-loader/8.0.6_@[email protected][email protected]/node_modules/babel-loader/lib/index.js):
  7. I gave up and switched to rush + npm.

NikitaIT avatar Sep 13 '19 11:09 NikitaIT

@NikitaIT i think problem in vue-cli, webpack doesn't provide ts-loader out of box

alexander-akait avatar Sep 13 '19 11:09 alexander-akait

@evilebottnawi i know, but i followed https://github.com/pnpm/pnpm/issues/1678 and this problem mark as related. I have Module not found: Error: Can't resolve 'ts-loader' when loader was already installed as a dependency for cli-plugin-typescript.

NikitaIT avatar Sep 13 '19 11:09 NikitaIT

It's a shame this hasn't been resolved yet. I love using pnpm because it treats dependencies in a sane way and it's fast. I also like the simplicity of vue-cli and the power of webpack.

Could we attempt to fix this somehow? I don't even know who should change.

This caught my eye in pnpm/pnpm#1678

I think the best/cleanest/most portable solution is to use resolveLoader.alias and require.resolve to map loaders directly to absolute paths. (i.e. config.resolveLoader.alias.set('babel-loader$', require.resolve('babel-loader')))

Any help is appreciated here @evilebottnawi .

wbern avatar Oct 16 '19 20:10 wbern

Have you tried to put a file .npmrc in your project root folder? The content is just like this:

shamefully-hoist=true

fengerzh avatar Oct 17 '19 03:10 fengerzh

@fengerzh in my case I'm using rush, which will break if I try to do that I think. So all the shameful options are not an option for me. In my monorepo I would prefer not to break the boundaries between the packages anyway.

wbern avatar Oct 17 '19 05:10 wbern

@wbern What is problem? webpack should works (some plugins/loaders/tools can don't work), what is error or example of error?

alexander-akait avatar Oct 17 '19 11:10 alexander-akait