【问题标题】:how to import function from a .js module to another .js module in node js, using express, ejs如何使用express,ejs将函数从.js模块导入节点js中的另一个.js模块
【发布时间】:2021-05-25 02:16:19
【问题描述】:

您好,我正在尝试将一个 js 文件中的函数导入另一个 js 文件,这些文件是节点 js 中的客户端,因此我无法使用 require 方法。 除非我在 package.json 文件中添加 type: module,否则我不能使用 import 语句,如果我这样做了,我必须找到一种方法来导入 ejs,因为我不能使用 require 方法在 package.json 中添加 type: module。有什么建议吗??

请在下面找到 app.js 的代码:

APP.JS

const express = require("express");
const app = express();

const ejs = require("ejs");

app.use("/public", express.static(__dirname + "/public"));
app.use("/public", express.static(__dirname + "/"));

【问题讨论】:

    标签: javascript node.js express ejs node-modules


    【解决方案1】:

    欢迎堆叠。问题不清楚。我可以添加一些可能对您有所帮助的示例。

    const getAlgoValue = (averageValue) => {
      let number = 3.5 / Math.pow(averageValue, 0.7) + 1.5;
      return number;
    };
    
    exports.getAlgoValue = getAlgoValue;

    这是我export的一个函数,我需要将它导入到另一个文件中才能使用它。

    const { getAlgoValue } = require('./getAlgo');

    希望对你有所帮助。一些可以帮助的资源module.exports

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多