简体   繁体   English

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

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

I'm having an issue trying to switch to yarn from npm inside my project.我在尝试从项目中的 npm 切换到纱线时遇到问题。 I tried downloading yarn and I'm still getting npm to start my project.我尝试下载纱线,但我仍然得到 npm 来启动我的项目。 I need to switch because a project I'm working on needs it to be yarn so I can add a maven frontend plugin to tie my backend and frontend to gather for deployment.我需要切换,因为我正在处理的项目需要它是纱线,所以我可以添加一个 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>

Link1 https://github.com/amigoscode/spring-boot-react-fullstack/blob/app-0/course-files/profiles.txt Link2 https://github.com/eirslett/frontend-maven-plugin Link1 https://github.com/amigoscode/spring-boot-react-fullstack/blob/app-0/course-files/profiles.txt Link2 https://github.com/eirslett/frontend-maven-plugin

Full simple step-by-step answer:完整的简单一步一步的答案:

  1. Install yarn npm i -g yarn安装纱npm i -g yarn
  2. Go to directory where u install packages and run yarn command Go 到你安装包并运行yarn命令的目录
  3. Yarn will init and create its yarn.lock file, now delete package-lock.json * Note Yarn 将初始化并创建它的yarn.lock文件,现在删除package-lock.json * Note
  4. In your package.json file replace in "scripts" all npm commands for yarn ** Note在您的package.json文件中,在"scripts"中替换所有npm yarn的命令** Note
  5. Run yarn dev or whatever command u use for running a yarn script => DONE运行yarn dev或你用来运行 yarn 脚本的任何命令=> DONE

* Note: it is important you don't delete it before yarn command (as some people suggest) it can break things, for example your yarn command will not even work and it will throw error: * Note:重要的是你不要在yarn命令之前删除它(正如一些人建议的那样)它会破坏一些东西,例如你的yarn命令甚至不会工作,它会抛出错误:

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

** Note: If you used ** Note:如果您使用

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

you can remove it, yarn is taking "resolutions" by default without a need for force:)你可以删除它,yarn 默认情况下会采取"resolutions" ,而不需要强制:)

It's quite simple;这很简单; follow those instructions:遵循这些说明:

  1. Verify that Yarn is installed on your machine globally, else use npm install -g yarn to install it.验证Yarn是否已全局安装在您的机器上,否则使用npm install -g yarn安装它。
  2. Go to your root project directory and type yarn on your CLI; Go 到您的项目根目录并在 CLI 上键入yarn Yarn can consume the same package.json format as npm, and can install any package from the npm registry. Yarn can consume the same package.json format as npm, and can install any package from the npm registry.

For more details, check the official yarn doc .有关更多详细信息,请查看官方 yarn 文档

You need not to do anything to switch from npm to yarn as long as yarn is installed in your Computer.只要您的计算机中安装了 yarn,您无需执行任何操作即可从 npm 切换到 yarn。 Also to install packages using yarn, you should run the command yarn rather than yarn install which is equivalent to npm install .同样要使用 yarn 安装包,你应该运行命令yarn而不是yarn install ,它等同于npm install Because yarn is nothing stand alone library.因为 yarn 不是一个独立的库。 Yarn is only a new CLI client that fetches modules from the npm registry. Yarn 只是一个新的 CLI 客户端,它从 npm 注册表中获取模块。 Nothing about the registry itself will change — you'll still be able to fetch and publish packages as normal.注册表本身不会发生任何变化——您仍然可以正常获取和发布包。

For more information about yarn you should read This有关纱线的更多信息,您应该阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何切换纱线版本/检查纱线的安装位置? - How do I switch Yarn versions/check where my Yarn is installed? 如果我的项目不在根目录中,如何从 Jenkins 执行 npm install 和 npm - How do I execute npm install and npm from Jenkins if my project is in not in the root directory 如何从 angular cli 中删除纱线。 我想用 npm 安装包 - How do I remove yarn from angular cli. I want to use npm to install packages 我可以将 React Native 应用程序从 Yarn 无缝切换到 NPM 还是它更复杂? - Can I switch React Native app from Yarn to NPM seamlessly or is it more involved? 当我在运行 npm 安装命令后收到错误时,如何从 github 克隆我的反应项目后运行 - How do I get my react project running after cloning it from github as I receive errors after running npm install command 如何使用 Node/NPM 在本地和远程依赖项之间切换? - How do I switch between local and remote dependencies with Node/NPM? 如何防止“ npm install”或“ npm remove”删除我的个人软件包? - How do I prevent “npm install” or “npm remove” from deleting my personal package? 强制项目使用 Yarn 但不使用 npm - Force project to use Yarn but not npm bootstrap 和 npm - 如何在我的项目中只导入“reboot.scss”文件? - bootstrap and npm - How do I import only the “reboot.scss” file in my project? 如何选择我的 npm 路径? - how do I select my npm path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM