How to import JavaScript files in NodeJS using file paths from project root? (instead of relative file paths)
In other words, my team is trying to transition from Syntax Style A to Syntax Style B (shown below):
Syntax Style A
const source = require('../../../../../../source.js');
Syntax Style B
const source = require('src/source.js'); // "src" is a direct subfolder of the project root
I have reviewed a lot of posts on this topic, and none of them can satisfy my project's requirements...
Below is my setup. Package.json was generated by WebStorm, and I haven't changed it.
Screenshot_of_Project_Structure
Project_Root
├─-─ src
│ └── a1
│ └── a2
│ └── a3
│ └── Worker.js
├──- subFolder1
│ └── SubWorker.js
├─ main.js
└─ package.json
main.js
const Worker = require('./src/a1/a2/a3/Worker.js');
Worker.doWork();
Worker.js
// If I replace the following with Project Root path, such as:
// const SubWorker = require('src/subFolder1/SubWorker.js');
// The script will fail with "Error: Cannot find module './src/subFolder1/SubWorker.js'."
const SubWorker = require('../../../subFolder1/SubWorker.js');
module.exports =
class Worker {
static doWork() {
console.log("doing work");
SubWorker.doMoreWork();
}
}
SubWorker.js
module.exports =
class SubWorker {
static doMoreWork() {
console.log("doing more work");
}
};
I tried the "path.resolve" solution, as suggested by this post:
NodeJS - convert relative path to absolute
app-module-path
solves all three requirements, but the program will not run (which is an implied requirement): ⛔Error: Cannot find module 'src/subFolder1/SubWorker.js'Worker.js
const rootPath = 'src/subFolder1';
const SubWorker = require(`${rootPath}/SubWorker.js`);
Since NodeJs is one of the most important backend languages, I was expecting excellent IDE support for imports... Referencing a source file from Project Root should be a simple operation that is handled by the IDE (I shouldn't need to use Babel.js or Webpack). Am I doing something wrong here?
I don't know what I am doing wrong, since I have already followed standard practices:
Thanks!
The Stack Overflow posts below are relevant, but do not address the specific issue in this post. (Please kindly search by the title on Stack Overflow, since the spam filter limited the number of links in this post, thanks!)
As of March 2023, a good way to eliminate the NodeJS relative paths is to use the imports
property in package.json
. (In the codes below, #root is the project root.)
// package.json
{
"imports": {
"#root/*.js": "./*.js"
}
}
// main.js:
const Source = require('#root/path/to/Source.js');
// Source.js:
module.exports = class Source {
// ...
}
// package.json:
{
"type" : "module",
"imports": {
"#root/*.js": "./*.js"
}
}
// main.js
import { Source } from '#root/path/to/Source.js';
// Source.js:
export class Source {
// ...
}
No need to "import" or "require" any additional packages (No Babel.js, No Webpack, No RequireJS). After installing NodeJS, this method works out of the box.
IDE linkages work as expected (Ctrl-click a class name to jump directly to the source file. Also, moving the source file (by drag and drop) will automatically update all references to the file. Tested on WebStorm 2022.3.2
and VS Code 1.76.2
.)
Works with both .mjs
(ECMAScript module system) and .cjs
(CommonJS) file types. Please see this reference Post on .cjs and .mjs.
No need to modify the reserved node_modules
directory
No need to set up any linux file links at the OS level