How to use import instead of require with node js?

Taylor Austin

Goal: To be able to use es6 import, export default, etc...

What I have done is include a .babelrc file in the root of my project which contains:

 {
   "presets": ["env", "react", "stage-0", "stage-1"],
   "plugins": ["transform-object-rest-spread"]
 }

I looked at the docs on babel and for node it said that all I would have to do is include the preset "env" which I do, but when I try to do an statement like:

import { data } from './data'

I get an unexpected token error on the import statement, so I assume I am not doing something correctly.

Muhammad Adeel

Typical webpack.config.js

module: {
    rules: [
        {
            test: /\.js$/,
            use: 'babel-loader',
            exclude: /node_modules/,
        },
        ...
    ]
}

I suggest you update .babelrc to the following.

{
    "presets": [
        [
          "env",
          {
            "modules": false,
            "loose": true
          }
        ],
        "react"
    ],
    "plugins": [
        "transform-object-rest-spread"
    ]
}

for more goodies decorators / function binding / class props etc,

{
    "presets": [
        [
          "env",
          {
            "modules": false,
            "loose": true
          }
        ],
        "react"
    ],
    "plugins": [
        "transform-object-rest-spread",
        "transform-function-bind",
        "transform-class-properties",
        "transform-decorators-legacy",
    ]
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

When to use "import" and when to use "require" in Node.JS

Gulp-sass 5.0 how to use compiler with import() instead of require()?

Why use import and require() in Node.js code?

How to make node.js require absolute? (instead of relative)

How to resolve require vs. import in a Node.js script

Azure Functions use import instead of require

Unable to use import instead of require, or unable to build dependensides with ts-node & tsdx

How to use import & export in node.js?

How to create an express Application with import instead of require

Node js require code instead of file

translate require() syntax to import syntax node js

How to use require() in a node.js project subfolder

How to use path in require method in react with node.js?

How do I change these require statements for these modules to use import statements instead?

How do I use require instead of import, while still being able to access properties of the variable?

How to use es6 import for importing electron instead of require function

Vue.js SSR - how to require vue-server-renderer with import instead?

How can I import a Typescript 1.0 function like a node.js require with arguments?

How to use 'require' to import a JSON in NestJS controller?

Node.js require vs React.js import

How to correctly import javascript function and use it into a node.js script?

what is use of require('.') in node.js

how to use require function in js

Node Require and Import

node.js require returns a string instead of module object

How to use "import" in node REPL

WebStorm Auto import - "require" instead of "import from"

How does require() in node.js work?

How to require a class file in node.js