USE pnp-auth instead
node-pnp-js allows you to use pnp-js-core from Node.js environment.
node-pnp-js implements it's own version of NodeFetchClient (shipped with sp-pnp-js) which supports authentication with help of node-sp-auth module.
npm install node-pnp-js --saveimport * as pnp from 'sp-pnp-js';
import NodeFetchClient from 'node-pnp-js';
pnp.setup({
sp: {
fetchClientFactory: () => {
return new NodeFetchClient(credentials);
}
}
});credentials - the same object (credentialOptions) provided for node-sp-auth module. That means you can use any authentication option from node-sp-auth you want.
new pnp.Web(siteUrl).get()
.then(data => {
console.log(`Your web title: ${data.Title}`);
})There are three different approaches you can use in order to provide your SharePoint site url.
pnp.setup({
sp: {
fetchClientFactory: () => new NodeFetchClient(test.creds)
}
});
new pnp.Web(siteUrl).get()
.then(data => {
console.log(`Your web title: ${data.Title}`);
})pnp.setup({
sp: {
fetchClientFactory: () => new NodeFetchClient(test.creds),
baseUrl: siteUrl
}
});
// now you can access your web using chaining syntax
// (pnp.sp.web will reference the web with url you provided as baseUrl):
pnp.sp.web.get()
.then(data => {
console.log(`Your web title: ${data.Title}`);
})pnp.setup({
sp: {
fetchClientFactory: () => new NodeFetchClient(test.creds)
}
});
// now you can access your web using chaining syntax
// (pnp.sp.web will reference the web with url you provided as siteUrl param):
pnp.sp.web.get()
.then(data => {
console.log(`Your web title: ${data.Title}`);
})- Any Node.js project with SharePoint. It can be remote jobs, azure functions, daemons, etc.
- Build pipeline extensibility. You can easily enhance your gulp pipeline with custom actions touching SharePoint.
- Anything else you can do with Node.js and SharePoint :)
I recommend using VS Code for development. Repository already contains some settings for VS Code editor.
git clone https://github.com/s-KaiNet/node-pnp-js.gitnpm installnpm run build- runs typescript compilation
- Rename file
/test/integration/private.config.sample.tstoprivate.config.ts. - Update information in
private.config.tswith appropriate values (urls, credentials). - Run
gulp test-int.

