在 Node.js 中,有三种常见的导入模块的方法:

  1. 使用 require 方法:这是使用最广泛的方法,通过 require 方法加载模块,例如:
const fs = require('fs');
  1. 使用 import 方法:这是 ES6 的模块导入方式,需要在文件头部添加 'use strict',并且需要将文件保存为 .mjs 格式,例如:
import fs from 'fs';
  1. 使用 module.exportsexports 对象:在模块中定义变量、函数时,可以使用 module.exportsexports 对象将其导出,例如:
module.exports = {foo: 'bar'};

exports.foo = 'bar';
Node.js 模块导入方法详解:require、import 和 exports

原文地址: https://www.cveoy.top/t/topic/oNab 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录