Switching Between Yarn and NPM
Intro
This post is not going to cover the differences between NPM and Yarn, or how to use them. We are just going to go over the process of switching from one to the other.
Things you can't delete
packge.json
Let's start by talking about the package.json file. Do not delete this file. Ever.
This file describes the dependencies that your app requires as well as startup scripts, and is used mutually by both Yarn and NPM. Do not ever delete this file.
Things you can delete
yarn.lock & package-lock.json files
These are the files that are created by NPM and Yarn respectively for their own purposes. These are auto generated and you can delete them at any time, and simply have them be automatically generated again. No worries.
These files are generated during the install process.
node_modules folder
This is where the actual packages live after they are installed. However you can install them as many times as you like. So don't worry about deleting this folder, it is automatically generated and can be regenerated easily.
This folder is generated during the install process.
Switching Between Package Managers
If you want to switch from NPM to Yarn follow these steps.
- delete the node_modules folder
- delete the package-lock.json file
- run
yarn install
This will generate a new node_modules folder and a yarn.lock file. Your project is now using yarn.
If you want to switch from Yarn to NPM follow these steps.
- delete the node_modules folder
- delete the yarn.lock file
- run
npm install
This will generate a new node_modules folder and a package-lock.json file. Your project is now using NPM.
Additional Items that may need changing
If you are using Docker you will also need to make updates to your Dockerfile.
For example a NPM based Dockerfile may look like this
FROM node:18-alpine
WORKDIR /app
EXPOSE 3001
COPY package*.json ./
# make node modules and give node user permission
RUN mkdir -p /app/node_modules
RUN chown node:node /app/node_modules
RUN npm install // highlight-line
COPY . .
# run as user node
USER node
CMD [ "npm", "start"] // highlight-line
while a Yarn based Dockerfile may look like this
FROM node:18-alpine
WORKDIR /app
EXPOSE 3001
COPY package*.json ./
# make node modules and give node user permission
RUN mkdir -p /app/node_modules
RUN chown node:node /app/node_modules
RUN yarn install // highlight-line
COPY . .
# run as user node
USER node
CMD [ "yarn", "start"] // highlight-line
Comments
Recent Work
Basalt
basalt.softwareFree desktop AI Chat client, designed for developers and businesses. Unlocks advanced model settings only available in the API. Includes quality of life features like custom syntax highlighting.
BidBear
bidbear.ioBidbear is a report automation tool. It downloads Amazon Seller and Advertising reports, daily, to a private database. It then merges and formats the data into beautiful, on demand, exportable performance reports.