30 lines
1.1 KiB
Plaintext
30 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
In Node.js, it's possible to import modules by specifying an absolute path, such as `/lib/foo/bar.js`. However, this approach can limit the portability of your code, as it becomes tied to your computer's file system. This could potentially lead to problems when the code is distributed, for instance, via NPM packages. Therefore, it's advisable to use relative paths or module names for importing modules to enhance the portability and compatibility of your code across different systems.
|
|
|
|
== How to fix it
|
|
|
|
Replace the absolute path with one that is relative to your current file.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,js,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
import { foo } from '/home/project/api/bar.js';
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,js,diff-id=1,diff-type=compliant]
|
|
----
|
|
import { foo } from '../api/bar.js';
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import[import]
|
|
* Node.js docs - https://nodejs.org/api/esm.html[ECMAScript modules]
|