如何在我的项目中从 npm 切换到 yarn?

How do I switch from npm to yarn in my project?

我在尝试从项目中的 npm 切换到 yarn 时遇到问题。我尝试下载 yarn,但仍然使用 npm 来启动我的项目。我需要切换,因为我正在处理的项目需要它是纱线,所以我可以添加一个 Maven 前端插件来连接我的后端和前端以收集部署。

1.
<frontend-maven-plugin.version>1.6</frontend-maven-plugin.version>
<node.version>v10.13.0</node.version>
<yarn.version>v1.12.1</yarn.version>

2.
<profiles>
    <profile>
        <id>demo</id>
        <build>
            <plugins>

                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <version>${frontend-maven-plugin.version}</version>
                    <configuration>
                        <workingDirectory>src/js</workingDirectory>
                    </configuration>
                    <executions>
                        <execution>
                            <id>install node</id>
                            <goals>
                                <goal>install-node-and-yarn</goal>
                            </goals>
                            <configuration>
                                <nodeVersion>${node.version}</nodeVersion>
                                <yarnVersion>${yarn.version}</yarnVersion>
                            </configuration>
                        </execution>
                        <execution>
                            <id>yarn install</id>
                            <goals>
                                <goal>yarn</goal>
                            </goals>
                            <phase>generate-resources</phase>
                        </execution>
                        <execution>
                            <id>yarn test</id>
                            <goals>
                                <goal>yarn</goal>
                            </goals>
                            <phase>test</phase>
                            <configuration>
                                <arguments>test</arguments>
                                <environmentVariables>
                                    <CI>true</CI>
                                </environmentVariables>
                            </configuration>
                        </execution>
                        <execution>
                            <id>yarn build</id>
                            <goals>
                                <goal>yarn</goal>
                            </goals>
                            <phase>compile</phase>
                            <configuration>
                                <arguments>build</arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <phase>process-classes</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>src/js/build</directory>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

链接1 https://github.com/amigoscode/spring-boot-react-fullstack/blob/app-0/course-files/profiles.txt 链接2 https://github.com/eirslett/frontend-maven-plugin

只要您的计算机中安装了 yarn,您就无需执行任何操作即可从 npm 切换到 yarn。另外,要使用 yarn 安装包,您应该 运行 命令 yarn 而不是 yarn install,后者等同于 npm install。因为 yarn 不是独立的库。 Yarn 只是一个新的 CLI 客户端,它从 npm 注册表中获取模块。注册表本身不会发生任何变化——您仍然可以像往常一样获取和发布包。

有关纱线的更多信息,您应该阅读 This

很简单;按照这些说明操作:

  1. 确认 Yarn 已全局安装在您的计算机上,否则使用 npm install -g yarn 进行安装。
  2. 转到您的项目根目录并在 CLI 上键入 yarnYarn 可以使用与 npm 相同的 package.json 格式,并且可以从 npm 注册表安装任何包。

有关详细信息,请查看 official yarn doc

完全简单step-by-step答案:

  1. 安装纱线npm i -g yarn
  2. 转到安装包的目录和运行 yarn命令
  3. Yarn 将初始化并创建其 yarn.lock 文件,现在您可以删除 package-lock.json * Note
  4. 在您的 package.json 文件中替换 "scripts" 所有 npm 命令 yarn ** Note
  5. 运行 yarn start 或您用于 运行 纱线脚本的任何命令 => DONE

* Note: 重要的是你不要在 yarn 命令之前删除它(正如一些人建议的那样)它会破坏东西,例如你的yarn 命令甚至不会工作,它会抛出错误:

Error: ENOENT: no such file or directory, open './package-lock.json'

** Note: 如果你用过

 "preinstall": "npx npm-force-resolutions",

你可以删除它,默认情况下 yarn 正在使用 "resolutions",无需强制:)