Walkthrough: Serializing with NuGet and npm
You can use package referencing (NuGet and/or npm) to import serialized items into your solution.
This walkthrough describes how to:
-
Create a package
-
Create a NuGet package
-
Create an npm package
-
Add a package reference
Create a package
To create a package containing Sitecore serialization:
-
Add a
sitecore.jsonfile in the package root. This file contains amodulesproperty pointing to serialization modules files.NoteModule files are not organized in a specific way in the package to accommodate different patterns.
An example of the package structure:
-
Root
-
sitecore.json -
modules-
items/*/*.module.json -
npm:./packageSrc/npm-resource-package -
nuget:[email protected]
-
-
An example of the
sitecore.jsonfile:RequestResponse{ "modules": [ "modules/*.module.json" ] }An example of a module file named
Sitecore.DevEx.NuGetResourcesExample.module.json:RequestResponse{ "namespace": "Sitecore.DevEx.NuGetResourcesExample", "description": "Content serialization example", "items": { "includes": [ { "name": "Content", "path": "/sitecore/content/home" } ] } } -
-
To check that you set up the package correctly, run the following command:
RequestResponsedotnet sitecore serialization infoYou set up the package successfully if the command returns the following message:
RequestResponseSitecore.DevEx.NuGetResourcesExample Content serialization example Subtrees: Content: /sitecore/content/home
Create a NuGet package
After you have added the sitecore.json file in the package root, you can create a NuGet package.
To create a NuGet package containing Sitecore resources:
-
Add the
PackageName.nuspecfile to define the package, for example:RequestResponse<?xml version="1.0"?> <package > <metadata> <id>NugetResourcesExample</id> <description>Example NuGet package containing Sitecore resources</description> <authors>Sitecore</authors> <version>1.0.0</version> </metadata> <files> <!-- the sitecore.json and modules and items must be copied to the package --> <file src="NuGetResourcesExample\sitecore.json" target="" /> <file src="NuGetResourcesExample\sitecore\**" target="sitecore" /> </files> </package>NoteThere is no specific location for the
PackageName.nuspecfile in your project, as long as the file paths are relative to it. -
Add any Sitecore resource files that you want to package (module files or serialized items) in the
filessection of thePackageName.nuspecfile. -
To build the package, run
nuget packand push it to the NuGet feed of your choice.
Create an npm package
After you have added the sitecore.json file in the package root, you can create an npm package.
To create an npm package containing Sitecore resources:
-
Add the
package.jsonfile to define the package, for example:RequestResponse{ "name": "npm-example-package", "version": "1.0.0", "description": "Sample of providing SIC resources via npm" } -
Open the
.gitignoreor.npmignorefiles to check that they do not ignore the resources you want to include automatically in the npm package.
You do not have to build most npm packages before publishing them to the package repository of your choice.
Add a package reference
To use package references, you must have nuget or npm available as a command on your PATH to install the packages during Sitecore CLI operations.
To add a package reference to your solution:
-
In the
sitecore.jsonfile, in themodulesproperty, add an entry for either:-
nuget:<packagename> -
npm:<packagename>
For example:
RequestResponse"modules": [ "local-file-modules/*.module.json", "npm:my-package", "npm:[email protected]", // version specs allowed per npm install "npm:./path/to/local/package", // local package on filesystem "nuget:Organization.MyPackage", "nuget:[email protected]", // version specs allowed. No wildcards. ],NoteNuGet package references install using registered package sources on your machine. To specify a custom package source, you can create a
NuGet.configfile at the root of your repository that contains any custom feeds to resolve your package references.For further information, see Extend the Command Line Interface with NuGet.
-
After a package reference is registered, it is automatically installed and parsed for modules during any Sitecore CLI operation that uses modules. You can check the module installation with sitecore ser info or sitecore ser push. Updating modules is automatic based on any version specifications.