To import JavaScript files in NodeJS using project root file paths instead of relative paths, you can set the NODE_PATH
environment variable to the root directory of your project.
Here are the steps to follow:
Open the terminal and navigate to the root directory of your project.
Set the NODE_PATH
environment variable to the root directory of your project by running the following command:
export NODE_PATH=./
This will set the NODE_PATH
to the current directory, which is the root of your project.
Create a src
directory in the root of your project, and place your JavaScript files in this directory.
In your code, you can import your JavaScript files using the project root file path as follows:
const myModule = require('src/myModule.js');
Note that you don't need to specify the relative path to the src
directory, as NodeJS will automatically look for the file in the directory specified by the NODE_PATH
environment variable.
Asked: 2022-02-03 11:00:00 +0000
Seen: 7 times
Last updated: Mar 18 '22