0

Please don't mark this as a duplicate of any Babel 6 questions, because this is specifically about Babel 7!

I'm trying out the new Babel 7 beta (@babel/polyfill@7.0.0-beta.32 etc.) and I'm having troubles using @babel/polyfill.

Summary

  • When I build the project with Webpack, everything works fine.
  • When I try to run it from the command-line, I get a SyntaxError.

I probably just forgot something simple, but I can't for the life of me figure out what. Any ideas what the problem is?

System spec

  • macOS High Sierra Developer Beta 5
  • Node 8.9.1
  • NPM 5.5.1

Files

.babelrc.js

module.exports = {
  presets: [
    ['@babel/preset-env', { targets: { node: 6 } }],
    ['@babel/preset-stage-3'],
    ['@babel/preset-flow']
  ]
}

src/index.js

require('@babel/polyfill')
module.exports = require('./main')

src/main.js

import 'dotenv-safe/config'

Command line

$ node --trace-warnings ./src/index.js

Error message

import 'dotenv-safe/config'
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/deniz/Code/my-project/src/index.js:3:18)

package.json

{
  "name": "foobar",
  "private": true,
  "version": "1.0.0",
  "description": "lol",
  "main": "./dist/bundle.js",
  "module": "./src/main.js",
  "scripts": {
    "clean": "rimraf dist",
    "build": "webpack",
    "watch": "webpack -w",
    "flow": "flow",
    "flow-typed": "flow-typed",
    "dev": "node --trace-warnings ./src/index.js",
    "start": "node ./dist/bundle.js"
  },
  "devDependencies": {
    "@babel/cli": "^7.0.0-beta.32",
    "@babel/core": "^7.0.0-beta.32",
    "@babel/preset-env": "^7.0.0-beta.32",
    "@babel/preset-flow": "^7.0.0-beta.32",
    "@babel/preset-stage-3": "^7.0.0-beta.32",
    "babel-eslint": "^8.0.2",
    "babel-loader": "^8.0.0-beta.0",
    "dotenv-safe": "^4.0.4",
    "eslint": "^4.12.0",
    "eslint-config-prettier": "^2.9.0",
    "eslint-plugin-flowtype": "^2.39.1",
    "eslint-plugin-prettier": "^2.3.1",
    "flow-bin": "^0.59.0",
    "flow-typed": "^2.2.3",
    "prettier": "^1.8.2",
    "rimraf": "^2.6.2",
    "webpack": "^3.9.1"
  },
  "dependencies": {
    "@babel/polyfill": "^7.0.0-beta.32",
    "node-schedule": "^1.2.5"
  }
}

webpack.config.js

const path = require('path')

module.exports = {
  target: 'node',
  entry: ['./src/index.js'],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        include: [path.resolve(__dirname, 'src')],
        loader: 'babel-loader'
      }
    ]
  },
  externals: [
    {
      mariasql: true,
      msnodesql: true,
      msnodesqlv8: true,
      mysql: true,
      mysql2: true,
      oracle: true,
      oracledb: true,
      'pg-query-stream': true,
      pg: true,
      sqlite3: true,
      'strong-oracle': true,
      tds: true
    }
  ],
  devtool: 'source-map'
}
damd
  • 6,116
  • 7
  • 48
  • 77
  • `SyntaxError: Unexpected token import` - Nodejs doen't support import yet, you should try `require('dotenv-safe/config')` – DSCH Dec 01 '17 at 13:10
  • @DSCH: Yeah, but that's why I'm doing `require('@babel/polyfill')` in index.js and then when I `require('./main.js')` I was under the impression I'd be able to use `import`? – damd Dec 01 '17 at 13:15
  • Btw, I hope you guys don't consider this a duplicate, because Babel 7 works a bit differently than Babel 6 when it comes to configuration, so the solution is not necessarily the same! – damd Dec 01 '17 at 13:16
  • AFAICT this has nothing to do with babel 6 v 7: you can't polyfill `import`. Period. You *have* to transpile it. Use one of the many babel plugins for that purpose. Also note that while the spec doesn't require it, every native implementation of modules ATM requires the `.js` file extension so if you wan't to use untranspiled in the browser you'll need to add it. – Jared Smith Dec 01 '17 at 13:24

1 Answers1

1

Try changing the entry point entry key to : entry: ['babel-polyfill', './src/index.js']. According to the docs the polyfill must be at the top of your entry point. https://babeljs.io/docs/usage/polyfill

DSCH
  • 2,146
  • 1
  • 18
  • 29