Skip to content
← Back to rules

import/no-absolute-path Suspicious

🚧 An auto-fix is planned for this rule, but not implemented at this time.

它的作用

此规则禁止使用绝对路径导入模块。

为什么这不好?

Node.js 允许使用绝对路径导入模块,例如 /home/xyz/file.js。 这是一种不好的做法,因为它会将使用它的代码绑定到你的计算机, 因此例如在 npm 上分发的软件包中就无法使用。

示例

此规则的错误代码示例:

js
import f from "/foo";
import f from "/some/path";
var f = require("/foo");
var f = require("/some/path");

此规则的正确代码示例:

js
import _ from "lodash";
import foo from "foo";
import foo from "./foo";

var _ = require("lodash");
var foo = require("foo");
var foo = require("./foo");

对于 { amd: true } 选项的错误代码示例:

js
define("/foo", function (foo) {});
require("/foo", function (foo) {});

对于 { amd: true } 选项的正确代码示例:

js
define("./foo", function (foo) {});
require("./foo", function (foo) {});

配置

此规则接受一个包含以下属性的配置对象:

amd

type: boolean

default: false

如果设置为 true,将解析 AMD 风格的 define 和 require 调用中的依赖路径:

js
/* import/no-absolute-path: ["error", { "commonjs": false, "amd": true }] */
define(["/foo"], function (foo) {
  /*...*/
}); // reported
require(["/foo"], function (foo) {
  /*...*/
}); // reported

const foo = require("/foo"); // ignored because of explicit `commonjs: false`

commonjs

type: boolean

default: true

如果设置为 true,将解析 CommonJS 风格 require 调用中的依赖路径:

js
var foo = require("/foo"); // reported

esmodule

type: boolean

default: true

如果设置为 true,将解析 ES module import 语句中的依赖路径:

js
import foo from "/foo"; // reported

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["import"],
  "rules": {
    "import/no-absolute-path": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["import"],
  rules: {
    "import/no-absolute-path": "error",
  },
});
bash
oxlint --deny import/no-absolute-path --import-plugin

版本

此规则在 v0.15.13 中新增。

参考