Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module using Yarn v. 3

I am following this installation guide and got error cannot find module - https://yarnpkg.com/getting-started/install

Here is repo - https://github.com/worldpwn/yarn-3-cannot-find-module-error

Step 1 Install Corepack

$ node -v
v16.13.0
$ corepack enable

Step 2 Initializing your project

$ yarn -v
1.22.15

$ yarn set version stable
➤ YN0000: Retrieving https://repo.yarnpkg.com/3.1.1/packages/yarnpkg-cli/bin/yarn.js
➤ YN0000: Saving the new release in .yarn/releases/yarn-3.1.1.cjs
➤ YN0000: Done in 0s 508ms

$ yarn -v
3.1.1

$ yarn init -2
{
  name: 'yarn-3-cannot-find-module-error',
  packageManager: '[email protected]'
}

Step 3 Install Lodash

$ yarn add lodash
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed 
➤ YN0000: ┌ Fetch step
➤ YN0013: │ lodash@npm:4.17.21 can't be found in the cache and will be fetched from the remote registry
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: Done in 0s 165ms

Step 4 Run hello world

index.js

console.log('hello world')
$ node index.js
hello world

Step 5 Use lodash - ERROR

index.js

var multiply = require('lodash/multiply');
console.log(multiply(3, 5))
$ node index.js
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'lodash/multiply'
Require stack:
- C:\Users\a.kniazev\Documents\GitHub\yarn-3-cannot-find-module-error\index.js
←[90m    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:778:27)←[39m
←[90m    at Module.require (node:internal/modules/cjs/loader:1005:19)←[39m
←[90m    at require (node:internal/modules/cjs/helpers:102:18)←[39m
    at Object.<anonymous> (C:\Users\a.kniazev\Documents\GitHub\yarn-3-cannot-find-module-error\index.js:1:16)
←[90m    at Module._compile (node:internal/modules/cjs/loader:1101:14)←[39m
←[90m    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)←[39m
←[90m    at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)←[39m {
  code: ←[32m'MODULE_NOT_FOUND'←[39m,
  requireStack: [
    ←[32m'C:\\Users\\a.kniazev\\Documents\\GitHub\\yarn-3-cannot-find-module-error\\index.js'←[39m
  ]
}

P.S. npm is working fine in other projects

If i will do npm i:

$ npm i

added 1 package, and audited 2 packages in 934ms

found 0 vulnerabilities

Everything works:

$ node index.js
15
like image 454
Andrei Kniazev Avatar asked Nov 01 '25 22:11

Andrei Kniazev


2 Answers

I figure out that the problem is that I have to run index.js using this command:

$ yarn node index.js
15

As I understand it uses PnP to resolve dependencies:

https://yarnpkg.com/features/pnp

https://yarnpkg.com/features/pnp#initializing-pnp

like image 130
Andrei Kniazev Avatar answered Nov 04 '25 01:11

Andrei Kniazev


Building on Andrei's answer (this is from https://yarnpkg.com/features/pnp#initializing-pnp):

As a quick tip, all yarn node typically does is set the NODE_OPTIONS environment variable to use the --require option from Node, associated with the path of the .pnp.cjs file. You can easily apply this operation yourself if you prefer:

node -r ./.pnp.cjs ./server.js
NODE_OPTIONS="--require $(pwd)/.pnp.cjs" node ./server.js
like image 25
djsavvy Avatar answered Nov 04 '25 01:11

djsavvy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!