0
npm install vue --save

Stuff gets installed and stored in package.json and package-lock.json, including:

"vue": "^2.5.21",   
"vue-router": "^3.0.2",
"vue-server-renderer": "^2.5.21",

But when I run

node src/main.js

with sourcecode (this is the full main.js file, 1 line)

import Vue from 'vue';

I get the error

src/main.js:1
(function (exports, require, module, __filename, __dirname) { import Vue from 'vue'
                                                                     ^^^

SyntaxError: Unexpected identifier

The import line is used in many Hello worlds, something simple must be wrong. Node version is v10.15.0. For example https://v2.vuejs.org/v2/guide/components-registration.html

8
  • Show your whole main.js file. Commented Jan 11, 2019 at 10:40
  • @Styx My whole main.js is there, it only consists of the import statement, the error is in line 1. Commented Jan 11, 2019 at 10:42
  • And node version? Commented Jan 11, 2019 at 10:43
  • 1
    Ah, sorry, I got confused as well. You should use const Vue = require('vue'); instead. Commented Jan 11, 2019 at 10:48
  • 2
    Vue code needs to run in the browser, not with node. Node is used to build Vue projects with webpack as a precompilcation step which bundles all modules together into code suitable for loading in the web browser (this is what vue-cli does). See Installation for more information. Commented Jan 11, 2019 at 11:09

1 Answer 1

2

NodeJS supports CommonJS (require/module.exports) modules only, not ES6 (import/export) you were trying to use.

Those examples you're referring to probable mention using babel transpiler. Under the hood it converts using ES6 modules to CommonJS ones, so node could work.

For example, you can create your test project like this:

npm -g i vue-cli
vue init webpack-simple vue-test-project
cd vue-test-project
npm i
npm run dev

This will install Babel transpiler, Webpack bundler and other needed packages. After that you can use ES6 modules as long as you run npm run dev (to run) or npm run build (to build).

Useful link to read: Vue.js → Installation


This is an answer on "Why import doesn't work?" question. As @DecadeMoon has pointed, vue is not supposed to be run in node.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.