section | title | description |
---|---|---|
using-npm | workspaces | Working with workspaces |
scope(7)
Workspaces
Description
Workspaces is a generic term that refers to the set of features in the npm cli that provides support to managing multiple packages from your local files system from within a singular top-level, root package.
This set of features makes up for a much more streamlined workflow handling linked packages from the local file system. Automating the linking process as part of npm install
and avoiding manually having to use npm link
in order to add references to packages that should be symlinked into the current node_modules
folder.
We also refer to these packages being auto-symlinked during npm install
as a single workspace, meaning it's a nested package within the current local file system that is explicitly defined in the package.json
workspaces
configuration.
Installing workspaces
Workspaces are usually defined via the workspaces
property of the package.json
file, e.g:
{ "name": "my-workspaces-powered-project", "workspaces": [ "workspace-a" ] }
Given the above package.json
example living at a current working directory .
that contains a folder named workspace-a
that disposes of a package.json
inside it, defining a nodejs package, e.g:
.
+-- package.json
`-- workspace-a
`-- package.json
The expected result once running npm install
in this current working directory .
is that the folder workspace-a
will get symlinked to the node_modules
folder of the current working dir.
Below is a post npm install
example, given that same previous example structure of files and folders:
.
+-- node_modules
| `-- workspace-a -> ../workspace-a
+-- package-lock.json
+-- package.json
`-- workspace-a
`-- package.json
Using workspaces
Given the specifities of how Node.js handles module resolution it's possible to consume any defined workspace by it's declared package.json
name
. Continuing from the example defined above, let's also create a Node.js script that will require the workspace-a
example module, e.g:
// ./workspace-a/index.js
module.exports = 'a'
// ./lib/index.js
const moduleA = require('workspace-a')
console.log(moduleA) // -> a
When running it with:
node lib/index.js
This demonstrates 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.
03fca6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this documentation rendered/hosted?
I searched all over the NPM documentation and can't find this. The blog post announcing Workspaces being released even didn't have any link to documentation...
I'm still new to workspaces, so without documentation on this new feature I'm really at a loss! π
03fca6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mesqueeb these are docs on the cli, so
npm help workspaces
should work if you're on v7.0.1+03fca6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jameswragg Oh I see. There was no mention of these documentations in the release blog of 7.0, nor is there any documentation on the website. I think people who are new to Workspaces might be very confused right now. Maybe the blog post can be edited so it says the only official documentation is through the CLI?
@isaacs @ruyadorno
03fca6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's also worth noting the possibility of rendering the built-in docs in a web browser with:
npm help workspaces --viewer=browser
(which just got a cleanup inv7.0.5
)@mesqueeb Thanks for the feedback, I'll pass it along to the team and make sure that we include more info about the docs in future comms π