Skip to content
← Back to rules

import/no-nodejs-modules Style

它的作用

禁止使用 Node.js 内置模块。对于无法访问这些模块的客户端 Web 项目来说,这可能很有用。

为什么这不好?

Node.js 内置模块(例如 fspathcrypto)在浏览器中不可用,因此在客户端 bundle 中导入它们会导致运行时失败,或者迫使打包工具注入沉重的 polyfill/shim。 这会增加 bundle 体积,可能将仅限服务端的逻辑泄漏到客户端,并且可能在生产环境之前掩盖运行环境不匹配的问题。

示例

以下是此规则的错误代码示例:

js
import fs from "fs";
import path from "path";

var fs = require("fs");
var path = require("path");

以下是此规则的正确代码示例:

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

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

/* import/no-nodejs-modules: ["error", {"allow": ["path"]}] */
import path from "path";

配置

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

allow

type: string[]

允许模块名称的数组。默认为空数组。

如何使用

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

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

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

版本

此规则在 v1.43.0 中添加。

参考资料