Find out how to serve an `index.html` HTML page using Node.js with no dependencies Show I had this problem with a Node.js script I wrote. I had set relative paths to reference some files in the local filesystem, like this:
and if I ran the program from the folder it was, no problem. But if I ran the file from another folder, from example the parent folder, the relative links would break. To fix this, at the beginning of the program, I set
This set the current working directory of the process to What you are trying to do is not possible. The reason for this is that in a POSIX system (Linux, OSX, etc), a child process cannot modify the environment of a parent process. This includes modifying the parent process's working directory and environment variables. When you are on the commandline and you go to execute your Node script, your current process ( There are a lot of reasons for this, but for one, imagine that you executed a script in the background (via If you need to perform some actions that require changing your working directory of your shell, you'll need to write a function in your shell. For example, if you're running Bash, you could put this in your
and then this cool thing is doable:
If you need to do more complex things in addition, you could always call out to your nodejs script from that function. This is the only way you can accomplish what you're trying to do. Skip to main content This browser is no longer supported. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Install NodeJS on Windows
In this articleIf you are new to developing with Node.js and want to get up and running quickly so that you can learn, follow the steps below to install Node.js directly on Windows. Note If you are using Node.js professionally, find performance speed and system call compatibility important, want to run Docker containers that leverage Linux workspaces and avoid having to maintain both Linux and Windows build scripts, or just prefer using a Bash command line, then install Node.js on Windows Subsystem for Linux (more specifically, WSL 2). Install nvm-windows, node.js, and npmBesides choosing whether to install on Windows or WSL, there are additional choices to make when installing Node.js. We recommend using a version manager as versions change very quickly. You will likely need to switch between multiple Node.js versions based on the needs of different projects you're working on. Node Version Manager, more commonly called nvm, is the most popular way to install multiple versions of Node.js, but is only available for Mac/Linux and not supported on Windows. Instead, we recommend installing nvm-windows and then using it to install Node.js and Node Package Manager (npm). There are alternative version managers to consider as well covered in the next section. Important It is always recommended to remove any existing installations of Node.js or npm from your operating system before installing a version manager as the different types of installation can lead to strange and confusing conflicts. This includes deleting any existing nodejs installation directories (e.g., "C:\Program Files\nodejs") that might remain. NVM's generated symlink will not overwrite an existing (even empty) installation directory. For help with removing previous installations, see How to completely remove node.js from Windows.)
Warning Access Denied Issue in nvm-windows version 1.1.9, switching node version requires elevated Powershell (run as administrator). It is recommended to use version 1.1.7 to avoid this issue.
Alternative version managersWhile windows-nvm is currently the most popular version manager for node, there are alternatives to consider:
To install Volta as your version manager (rather than windows-nvm), go to the Windows Installation section of their Getting Started guide, then download and run their Windows installer, following the setup instructions. Important You must ensure that Developer Mode is enabled on your Windows machine before installing Volta. To learn more about using Volta to install multiple versions of Node.js on Windows, see the Volta Docs. Install Visual Studio CodeWe recommend you install Visual Studio Code, as well as the Node.js Extension Pack, for developing with Node.js on Windows. Install them all or pick and choose which seem the most useful to you. To install the Node.js extension pack:
A few additional extensions you may want to consider include:
Alternative code editorsIf you prefer to use a code editor or IDE other than Visual Studio Code, the following are also good options for your Node.js development environment:
Install GitIf you plan to collaborate with others, or host your project on an open-source site (like GitHub), VS Code supports version control with Git. The Source Control tab in VS Code tracks all of your changes and has common Git commands (add, commit, push, pull) built right into the UI. You first need to install Git to power the Source Control panel.
Use Windows Subsystem for Linux for productionUsing Node.js directly on Windows is great for learning and experimenting with what you can do. Once you are ready to build production-ready web apps, which are typically deployed to a Linux-based server, we recommend using Windows Subsystem for Linux version 2 (WSL 2) for developing Node.js web apps. Many Node.js packages and frameworks are created with a *nix environment in mind and most Node.js apps are deployed on Linux, so developing on WSL ensures consistency between your development and production environments. To set up a WSL dev environment, see Set up your Node.js development environment with WSL 2. FeedbackSubmit and view feedback for |