0

我正在遵循此安装指南,但出现错误找不到模块 - https://yarnpkg.com/getting-started/install

这是回购 - https://github.com/worldpwn/yarn-3-cannot-find-module-error

步骤 1 安装 Corepack

$ node -v
v16.13.0
$ corepack enable

步骤 2 初始化您的项目

$ 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: 'yarn@3.1.1'
}

步骤 3 安装 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

第 4 步运行 hello world

index.js

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

第 5 步使用 lodash - 错误

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
  ]
}

PS npm 在其他项目中运行良好

如果我会做 npm 我:

$ npm i

added 1 package, and audited 2 packages in 934ms

found 0 vulnerabilities

一切正常:

$ node index.js
15
4

1 回答 1

0

我发现问题在于我必须使用以下命令运行 index.js:

$ yarn node index.js
15

据我了解,它使用 PnP 来解决依赖关系:

https://yarnpkg.com/features/pnp

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

于 2021-12-22T09:10:24.157 回答