The npm has made been a popular package manager that has got itself as a preferred option in the coding and development domain. The software which is a default package manager for the JavaScript runtime environment Node.js saw the release of its latest version last year. The latest update came with workspace or what is now called npm workspaces.

While you might have thought that it is a complex thing associated with node js website development then hang on. By clicking the URL to check here about the same you have made the right move. As the title has given you the hint we are here to discuss the npm workspaces right from the basic so, that all your doubts get vanished.  So, lets get started.

The Workspaces

As per the official website of the npm, Workspaces has been used as a generic term. The term is used to define the features of npm cli. The features provide support to managing multiple packages from your local files system from within a singular top-level, root package.

Workspaces is a good way to work with the monorepos. The monorepos are repositories that have multiple packages that is more than one package.json file. Now, what npm workspaces have bought is not a new thing that has come to node js website development. Check here for node.js website development services. In fact, package managers like pnpm and Yarn have already done that.

Setting Up The npm Workspaces 

Having discussed the basics about the npm and the latest version of it with npm workspaces let’s move to the other part. We would now take a look at how to set up npm’s latest version. So, check it and follow the steps.

Install/Update: If you have been using the npm then just update the same to a newer version. If you are not using then you need to install the latest version 7 of the software. To update run the command:

npm install -g [email protected] on terminal

The new Node.js 15 comes in with the latest version of the npm. You would need a need a package.json file in the root of your repository to install the latest version.

Defining Workspace: Defining workspaces is done through the property of the package.json file. The basic example of defining the workspaces is as follows:

{

“name”: “my-workspaces-powered-project”,

“workspaces”: [

“workspace-a”

]

}

The example above shows a package.json file living in a current working directory . that contains a folder named workspace-a that disposes of a package.json inside it.

To define the node.js do as follows:

+– package.json

`– workspace-a

`– package.json

Post this when you run npm install in this current working directory . then the folder workspace-a will get symlinked to the node_modules folder of the current working dir.

Post-install defining of node.js is as follows:

+– node_modules

|  `– workspace-a -> ../workspace-a

+– package-lock.json

+– package.json

`– workspace-a

`– package.json

Working With npm 

Having installed and defined the npm latest version we now take a look at how to start working with it. Use the command:

npm init -w ./packages/a

This would create the missing folders and a new package.json file (if needed). Further, it would properly configure the “workspaces” property of your root project package.jso.

Dependencies 

In Workspaces, it is easy to add/remove/update dependencies. Here is an example of that.

+– package.json

`– packages

+– a

|   `– package.json

`– b

`– package.json

To add dependency named abbrev from the registry as a dependency of your workspace a, you may use the workspace config to tell the npm installer that package should be added as a dependency. This can be done with

npm install abbrev -w a

New Node.js Script

To create a new Node.js script with workspace-a follow the below given example:

// ./workspace-a/index.js

module.exports = ‘a’

// ./lib/index.js

const moduleA = require(‘workspace-a’)

console.log(moduleA) // -> a

Run it with: node lib/index.js

The above example explains well that how the nature of node_modules resolution allows for workspaces to enable a portable workflow for requiring each workspace in such a way that is also easy to publish these nested workspaces to be consumed elsewhere.

Getting The Commands Running 

To run the commands in the context of a configured workspace you can use the workspace configuration option.

Here is an example to use the npm run command in the context of nested workspaces, containing multiple workspaces.

+– package.json

`– packages

+– a

|   `– package.json

`– b

`– package.json

In order to run the above-given command in the context of that specific workspace, you can use the command:

npm run test –workspace=a

The command would run the test script defined within the ./packages/a/package.json file.

In order to specify this argument multiple times in the command-line in order to target multiple workspaces use:

npm run test –workspace=a –workspace=b

Further, you can use the workspaces (plural) configuration option to enable the same behavior but running that command in the context of all configured workspaces. An example of this is as follows:

npm run test –workspaces

The command runs the test script in both ./packages/a and ./packages/b.

Conclusion 

So, as we come to the end of our discussions here we hope all the things that you wanted to know are addressed. The latest version of the npm called the workspaces has created a buzz and it is natural to be getting attracted to the hype. As we saw the workspaces bring in something that already exists in other package managers it is a change in npm that was long due.

The developers have welcomed the change as npm has a big community of users. As we saw that using, defining and setting up the Workspaces is not that difficult we hope you would be at ease after reading this. Don’t forget to let us know what more topic you want us to write so as to help solve your queries. Keep sending in your suggestions until we meet the next time.  If you are looking for psd to html ,convert psd to html5 , convert psd to html5 services then you can contact html development company . AIS Technolabs designers are knowledgeable in dealing with pre labels of HTML language, which are fundamental for making effective site pages. Our PSD to HTML change benefits just upgrade the usefulness of the PSD report by changing it over to codes.

LEAVE A REPLY

Please enter your comment!
Please enter your name here