简体   繁体   English

export function 和 module.exports 有什么区别?

[英]What is the difference between export function and module.exports?

I have a Javascript file utils.js which contains some utility functions.我有一个 Javascript 文件utils.js ,其中包含一些实用程序功能。 Here is an example:这是一个例子:

export function RemoveHTML(Str) {
    return Str.replace(/<[^>]*(?:>|$)/gi,'');
}

I can use these functions by importing the utils.js file like such:我可以通过像这样导入utils.js文件来使用这些功能:

import {RemoveHTML} from '../js/utils.js';

I also have a model.js file for some data queries like this (pseudo-code for brevity):我还有一个model.js文件,用于像这样的一些数据查询(为简洁起见,伪代码):

async function getStuff() {
    await DBConnection.connect();
    return results
}

module.exports = {
  getStuff
}

For 'consistency' I thought I'd change model.js to just this:对于“一致性”,我想我model.js更改为:

export async function getStuff() {
    await DBConnection.connect();
    return results
}

If I do that then I get an app crash error stating:如果我这样做,那么我会收到一个应用程序崩溃错误,说明:

SyntaxError: Unexpected token 'export' SyntaxError:意外的令牌“导出”

What is the difference between exporting a function using export function() and module.exports ?使用export function()module.exports导出 function 有什么区别?

UPDATE:更新:

I am using Babel in Webpack like below so why would I get an error?:我在Webpack中使用Babel ,如下所示,为什么会出现错误?:

{
      test: /\.js$/,
        include: [ srcPath ],
      exclude: ['/node_modules/'],
      use: {
        loader: 'babel-loader',
        options: {
          presets: ["@babel/preset-env"]  //Preset used for env setup
        }
      }
    },

export function() is an ES6 syntax used for exporting while module.exports is or exports is a special object which is included in every JS file in the Node.js application by default. export function()是用于exportsES6语法,而module.exports是或 export 是一个特殊的 object,默认情况下它包含在Node.js应用程序的每个 JS 文件中。
You can also use the ES6 syntax for exporting/importing a module in Node.js but for using that you will have to transpile the new ES6 code to Node.js supported ES5 format, You can easily do that using babel (which is a tool for transpiling JavaScript).您还可以使用 ES6 语法在Node.js中导出/导入模块,但要使用它,您必须将新的ES6代码转译为 Node.js 支持的ES5格式,您可以使用babel (这是一个转译 JavaScript 的工具)轻松做到这一点.

@JonasWilms is definitely correct about the point he made. @JonasWilms 关于他的观点绝对正确。 I see you are using commonjs on the server code and es6 on the client-side.我看到您在服务器代码上使用 commonjs,在客户端使用 es6。

There is no difference between module.export or export. module.export 或 export 之间没有区别。 In your project, your server code is using commonjs modules, so you should use module.exports.在您的项目中,您的服务器代码正在使用 commonjs 模块,因此您应该使用 module.exports。 In your client code, keep on using export (es6) syntax of JavaScript.在您的客户端代码中,继续使用 JavaScript 的导出 (es6) 语法。

But if you want to write your javascript globally with es6, you will have to install some dependencies and configure your babel.但是如果你想用 es6 全局编写你的 javascript,你将不得不安装一些依赖项并配置你的 babel。

Check out this link https://www.codementor.io/@iykyvic/writing-your-nodejs-apps-using-es6-6dh0edw2o查看此链接https://www.codementor.io/@iykyvic/writing-your-nodejs-apps-using-es6-6dh0edw2o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM