DEV Community

WilliamRamirez
WilliamRamirez

Posted on

1

Control Node.js versions with Node Version Manager (NVM) to develop SPFx solutions for different SharePoint environments

NVM for Windows

The Microsoft/npm/Google recommended Node.js version manager for Windows.

NVM (Node Version Manager) is a tool that allows you to install and switch between different versions of Node.js on your machine. This can be useful for developing SPFx (SharePoint Framework) solutions that require specific Node.js versions. For example, SPFx v1.17.1 requires Node.js v16 LTS, while SPFx v1.4.1 for SharePoint Server 2019 requires Node.js v8 LTS .

To use NVM for SPFx development, you need to follow these steps :

  • Install NVM from https://github.com/coreybutler/nvm-windows/releases or use choco install nvm if you have Chocolatey installed.
  • Run nvm install <version> to install the desired Node.js versions, such as nvm install 16.13.0 and nvm install 8.17.0.
  • Run nvm use <version> to switch between Node.js versions, such as nvm use 16.13.0 or nvm use 8.17.0.
  • Install the SPFx development tools with npm install -g yo @microsoft/generator-sharepoint gulp.
  • Create a new SPFx project with yo @microsoft/sharepoint and follow the prompts.
  • Run gulp serve to start the local web server and test your SPFx solution.

Using NVM can help you avoid compatibility issues and errors when developing SPFx solutions for different SharePoint environments.

I did an example after running nvm install 16.18.0 and nvm use 16.18.0:

1) I ran in the Powershell cli:

npm install gulp-cli yo @microsoft/generator-sharepoint --global
Enter fullscreen mode Exit fullscreen mode

2) Created boiler plate web part code

yo @microsoft/sharepoint
Enter fullscreen mode Exit fullscreen mode

3) Updated SharePoint URL

Locate and open the file ./config/serve.json in your project.

{
  "$schema": "https://developer.microsoft.com/json-schemas/core-build/serve.schema.json",
  "port": 4321,
  "https": true,
  "initialPage": "https://enter-your-SharePoint-site/_layouts/workbench.aspx"
}
Enter fullscreen mode Exit fullscreen mode

4) Trust local environment.

gulp trust-dev-cert
gulp serve

or  you can use 

gulp serve --nobrowser
Enter fullscreen mode Exit fullscreen mode

5) Here is the result

Image description

Keep learning, testing, and deploying. Then, repeat. Have fun!

Source:
Setup dev environment

Webpart hello world

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (4)

Collapse
 
jaloplo profile image
Jaime López

Nice article!!!!

One quick question from here: do you need to specify the @microsoft/generator-sharepoint version package depending on the Node.js version you currently are or does it do it automatically?

Collapse
 
williamramirez profile image
WilliamRamirez

The answer is that the package does not automatically detect the Node.js version and adjust accordingly. You need to manually specify the version of the package that matches your Node.js version. For example, if you are using Node.js 10.x, you need to use @microsoft/generator-sharepoint@1.12.1 or higher. If you are using Node.js 14.x, you need to use @microsoft/generator-sharepoint@1.13.0 or higher. This ensures that you have the latest features and bug fixes for your Node.js environment.

Collapse
 
jaloplo profile image
Jaime López

Thanks!!!

Do you have a list with the relationship of the packages and tools versions? Like the one above that you answered. It would be really useful when working with some kind of "legacy" (understood in terms of old SPFX versions) developments.

Thread Thread
 
williamramirez profile image
WilliamRamirez

Here is the Microsoft documentation URL related to that.
SharePoint Framework development tools and libraries compatibility

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay