gpt4 book ai didi

yarnpkg - 无法使用 Yarn v.3 找到模块

转载 作者:行者123 更新时间:2023-12-05 04:39:27 64 4
gpt4 key购买 nike

我正在按照此安装指南进行操作,但遇到找不到模块的错误 - https://yarnpkg.com/getting-started/install

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

第一步安装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'
}

第三步安装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

第四步运行hello world

索引.js

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

第 5 步使用 lodash - 错误

索引.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
]
}

附言npm 在其他项目中运行良好

如果我要执行 npm i:

$ npm i

added 1 package, and audited 2 packages in 934ms

found 0 vulnerabilities

一切正常:

$ node index.js
15

最佳答案

我发现问题是我必须使用此命令运行 index.js:

$ yarn node index.js
15

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

https://yarnpkg.com/features/pnp

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

关于yarnpkg - 无法使用 Yarn v.3 找到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70446257/

64 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com