How can I insert a file in SharePoint online using PnP library?

enter image description here

enter image description here

enter image description here

It is throwing error that getFolderByServerRelativeUrl does not exist.

1 Answer 1


It looks like you need to import { sp } from "@pnp/sp", in order to use getFolderByServerRelativeUrl.

import { sp } from "@pnp/sp";

sp.web.getFileByServerRelativeUrl("/sites/dev/documents/file.avi").getBlob().then((blob: Blob) => {});

sp.web.getFileByServerRelativeUrl("/sites/dev/documents/file.avi").getBuffer().then((buffer: ArrayBuffer) => {});

sp.web.getFileByServerRelativeUrl("/sites/dev/documents/file.json").getJSON().then((json: any) => {});

sp.web.getFileByServerRelativeUrl("/sites/dev/documents/file.txt").getText().then((text: string) => {});

// all of these also work from a file object no matter how you access it
sp.web.getFolderByServerRelativeUrl("/sites/dev/documents").files.getByName("file.txt").getText().then((text: string) => {});


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.