Hunter
Hunter

Reputation: 458

Can't get Node App Started (server.js, possible Babel issue?)

I'm trying to get my Node application to start with npm start. The application is crashing on start and I do not know why. I am really new to Node and all things full-stack. Can anybody make sense of whats going on?

When I run npm start --verbose I get this:

hunterhawley@hunter-test:~$ npm start --verbose
npm info it worked if it ends with ok
npm verb cli [ '/home/hunterhawley/.nvm/versions/node/v11.10.1/bin/node',
npm verb cli   '/home/hunterhawley/.nvm/versions/node/v11.10.1/bin/npm',
npm verb cli   'start',
npm verb cli   '--verbose' ]
npm info using [email protected]
npm info using [email protected]
npm verb config Skipping project config: /home/hunterhawley/.npmrc. (matches userconfig)
npm verb run-script [ 'prestart', 'start', 'poststart' ]
npm info lifecycle [email protected]~prestart: [email protected]
npm info lifecycle [email protected]~start: [email protected]

> [email protected] start /home/hunterhawley
> cross-env BABEL_DISABLE_CACHE=1 NODE_ENV=development nodemon index.js

[nodemon] 1.17.5
[nodemon] reading config ./nodemon.json
[nodemon] to restart at any time, enter `rs`
[nodemon] or send SIGHUP to 31462 to restart
[nodemon] ignoring: .git node_modules/**/node_modules
[nodemon] watching: /home/hunterhawley/server/**/* Intl
[nodemon] watching extensions: js,json
[nodemon] starting `node index.js`
[nodemon] forking
[nodemon] child pid: 31475
[nodemon] watching 37 files
/home/hunterhawley/node_modules/babel-core/lib/transformation/file/index.js:558
      throw err;
      ^

SyntaxError: /home/hunterhawley/server/server.js: Unexpected token (164:8)
  162 |     const store = configureStore({
  163 |       app: {
> 164 |         ...initialAppState,
      |         ^
  165 |         user: { ...req.session.user },
  166 |       },
  167 |     });
    at Parser.pp$5.raise (/home/hunterhawley/node_modules/babylon/lib/index.js:4454:13)
    at Parser.pp.unexpected (/home/hunterhawley/node_modules/babylon/lib/index.js:1761:8)
    at Parser.pp$3.parseIdentifier (/home/hunterhawley/node_modules/babylon/lib/index.js:4332:10)
    at Parser.pp$3.parsePropertyName (/home/hunterhawley/node_modules/babylon/lib/index.js:4156:96)
    at Parser.pp$3.parseObj (/home/hunterhawley/node_modules/babylon/lib/index.js:4045:12)
    at Parser.pp$3.parseExprAtom (/home/hunterhawley/node_modules/babylon/lib/index.js:3719:19)
    at Parser.pp$3.parseExprSubscripts (/home/hunterhawley/node_modules/babylon/lib/index.js:3494:19)
    at Parser.pp$3.parseMaybeUnary (/home/hunterhawley/node_modules/babylon/lib/index.js:3474:19)
    at Parser.pp$3.parseExprOps (/home/hunterhawley/node_modules/babylon/lib/index.js:3404:19)
    at Parser.pp$3.parseMaybeConditional (/home/hunterhawley/node_modules/babylon/lib/index.js:3381:19)
[nodemon] app crashed - waiting for file changes before starting...

So it seems to me that this is a babel issue. I can attach my index.js or any other file if that will help you figure this out. Thank you!

EDIT: Also I just wanted to add that the install process (though it took some work) went smoothly, and I have MongoDB running in another terminal window, which for this application is needed. I've also tried going direct and running node index.js but that didn't get me anywhere either.

EDIT 2: Here is what I got when installing the plugin given in answer 1

npm WARN [email protected] requires a peer of webpack@>=1.12.9 <3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of webpack@^1.4.0-beta6 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of webpack@^1.9.11 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of webpack@^1.0.0 || ^2.0.0 || ^3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of webpack@^2.2.0 || ^3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of webpack@1 || 2 || 3 but none is installed. You must install peer dependencies yourself.
npm WARN @babel/[email protected] requires a peer of @babel/core@^7.0.0-0 but none is installed. You must install peer dependencies yourself.
npm WARN @babel/[email protected] requires a peer of @babel/core@^7.0.0-0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No repository field.

+ @babel/[email protected]
added 3 packages from 1 contributor and audited 20460 packages in 22.759s

EDIT 3: Adding my webpack.config.babel.js file

var cssnext = require('postcss-cssnext');
var postcssFocus = require('postcss-focus');
var postcssReporter = require('postcss-reporter');

var cssModulesIdentName = '[name]__[local]__[hash:base64:5]';
if (process.env.NODE_ENV === 'production') {
  cssModulesIdentName = '[hash:base64]';
}

module.exports = {
  output: {
    publicPath: '/',
    libraryTarget: 'commonjs2',
  },
  resolve: {
    extensions: ['', '.js', '.jsx'],
    modules: [
      'client',
      'node_modules',
    ],
  },
  module: {
    loaders: [
      {
        test: /\.css$/,
        exclude: /node_modules/,
        loader: 'style-loader!css-loader?localIdentName=' + cssModulesIdentName + '&modules&importLoaders=1&sourceMap!postcss-loader',
      },
      {
        test: /\.jpe?g$|\.gif$|\.png$|\.svg$/i,
        loader: 'url-loader?limit=10000',
      },
    ],
  },
  postcss: () => [
    postcssFocus(),
    cssnext({
      browsers: ['last 2 versions', 'IE > 10'],
    }),
    postcssReporter({
      clearMessages: true,
    }),
  ],
};

Upvotes: 0

Views: 220

Answers (1)

cantuket
cantuket

Reputation: 1592

Easiest solution is update to Node v8.3 or higher...

"As of Node v8.3, object rest/spread is available without the need for any transpilation."

If you're transpiling using Babel, as you mentioned, then you'll need to add a plugin to polyfill support for Object spread. In your .babelrc file add...

"plugins": [
      "@babel/plugin-proposal-object-rest-spread"
]

And you'll need to install this plugin obviously...

npm i -D @babel/plugin-proposal-object-rest-spread
//OR
yarn add @babel/plugin-proposal-object-rest-spread --dev

NOTE: This will require "@babel/core": "^7.0.0"

Here is an example of how to configure your .babelrc file for node transpiling...

  {
    "presets": [
        ["@babel/env", {
          "targets": {
            "node": "current"
          }
        }]
    ],
    "plugins": [
      "@babel/plugin-proposal-object-rest-spread",
      "@babel/plugin-proposal-class-properties"
    ]
  }

You can also add the plugin directly within you Webpack config (webpack.config.babel.js)...

loaders: [
 {
  test: /\.js$/,
  exclude: /node_modules/,
  use: {
    loader: 'babel-loader',
    options: {
      presets: ['@babel/preset-env'],
      plugins: ['@babel/plugin-proposal-object-rest-spread']
    }
  }
 }
]

Example of specific packages (in package.json) I'm currently using in a Node/Babel project, which may help upgrading to Babel ^7.0.0...

"devDependencies": {
 "@babel/cli": "^7.2.3",
 "@babel/core": "^7.2.2",
 "@babel/plugin-proposal-object-rest-spread": "^7.3.1",
 "@babel/plugin-proposal-class-properties": "^7.3.4",
 "@babel/preset-env": "^7.3.1",
 "babel-core": "7.0.0-bridge.0",
 "babel-eslint": "^10.0.1",
 "babel-jest": "^24.0.0",
 "babel-loader": "^8.0.5",
 "babel-plugin-module-resolver": "^3.2.0"
}

Upvotes: 2

Related Questions