Using Import In NodeJS server

前端 未结 2 387
孤街浪徒
孤街浪徒 2020-12-23 23:25

At the moment all my module in my nodejs server are imported as require() ie:

let path = require(\'path\');
let express = require(\'express\');
let http = re         


        
相关标签:
2条回答
  • 2020-12-24 00:01

    It works in the webpack situation because the code is run through babel. You can run your node.js code through babel.

    Install the babel cli if you don't have it

    npm install --save-dev babel-cli
    

    Then run your code like this:

    ./node_modules/.bin/babel-node server.js
    

    Or put it in package.json.

    {
      "scripts": {
        "start": "babel-node server.js"
      }
    }
    
    0 讨论(0)
  • 2020-12-24 00:08
    By default, you’ll be using ES5 and it’ll be required to use require (ja ja) to pull in modules. As we move forward with ES6 and beyond, it’s really best for us to start using ES6 classes as well as import and export statements. To do this, we’ll need Babel in order to interpret our ES6 syntax.
    
    1. npm install --save-dev babel-cli
    
    2. npm install --save-dev babel-preset-es2015
    
    3. Lets pull in both ‘babel-cli and babel preset es2015” as dev dependencies as well as add the .babelrc file
    
      {
        "presets": ["es2015"]
      }
    
    The issue was gone, if you do as above steps`enter code here`
    
    more infor please see:https://codebrains.io/setting-up-express-with-es6-and-babel/
    
    0 讨论(0)
提交回复
热议问题