javascript - What is the difference between export function and module.exports? - Stack Overflow

I have a Javascript file utils.js which contains some utility functions. Here is an example:export func

I have a Javascript file utils.js which contains some utility functions. 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:

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

I also have a model.js file for some data queries like this (pseudo-code for brevity):

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

module.exports = {
  getStuff
}

For 'consistency' I thought I'd change model.js to just this:

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'

What is the difference between exporting a function using export function() and module.exports?

UPDATE:

I am using Babel in Webpack like below so why would I get an error?:

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

I have a Javascript file utils.js which contains some utility functions. 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:

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

I also have a model.js file for some data queries like this (pseudo-code for brevity):

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

module.exports = {
  getStuff
}

For 'consistency' I thought I'd change model.js to just this:

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'

What is the difference between exporting a function using export function() and module.exports?

UPDATE:

I am using Babel in Webpack like below so why would I get an error?:

{
      test: /\.js$/,
        include: [ srcPath ],
      exclude: ['/node_modules/'],
      use: {
        loader: 'babel-loader',
        options: {
          presets: ["@babel/preset-env"]  //Preset used for env setup
        }
      }
    },
Share Improve this question edited May 1, 2020 at 16:58 volume one asked May 1, 2020 at 16:22 volume onevolume one 7,58314 gold badges90 silver badges173 bronze badges 3
  • 2 export and import are es6 features not supported by nodejs natively(depending upon the version). either use babel to transpile or maybe set package.json type module or use mjs(again depending upon the nodejs version) – Aritra Chakraborty Commented May 1, 2020 at 16:26
  • 3 export syntax is new, and not yet supported by NodeJS. module.exports is the way NodeJS is providing modules, so that will work natively. – Jonas Wilms Commented May 1, 2020 at 16:26
  • I am using Babel in my Webpack build. So I shouldn't be getting this error right? – volume one Commented May 1, 2020 at 16:32
Add a ment  | 

2 Answers 2

Reset to default 4

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.
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).

@JonasWilms is definitely correct about the point he made. I see you are using monjs on the server code and es6 on the client-side.

There is no difference between module.export or export. In your project, your server code is using monjs modules, so you should use module.exports. In your client code, keep on using export (es6) syntax of JavaScript.

But if you want to write your javascript globally with es6, you will have to install some dependencies and configure your babel.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745393783a4625784.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信