Trouble running hello.js

curiouswebster
curiouswebster used Ask the Experts™
on
This is my hello.js file:

console.log(“Angular works on the Mac”);

I installed Angular, but an trying to verify it works. What's wrong here?

$ node hello.js
/Users/newbieweb/Desktop/Dev/Angular/udemy-dream-app/hello.js:1
(function (exports, require, module, __filename, __dirname) { console.log(“Angular works on the Mac”);
                                                                          ^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:588:28)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:607:3
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Justin EstradaWordPress Developer

Commented:
You are probably just putting your console.log code in the wrong place.
Inside your app.component.ts you can run a console.log inside the constructor or import { OnInit } from '@angular/core';
And you can place your console.log inside your ngOnInit( )

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

	constructor () {
console.log("Angular works on the Mac");
       }

	ngOnInit( ) {
console.log("Angular works on the Mac");
	}

}

Open in new window

curiouswebsterSoftware Engineer

Author

Commented:
No, same problem with your code.

Mine works fine on my Angular Windows installation.

There is some installation issue or a path issue.
curiouswebsterSoftware Engineer

Author

Commented:
$ node hello.js
/Users/newbieweb/Desktop/Dev/Angular/udemy-dream-app/hello.js:1
(function (exports, require, module, __filename, __dirname) { import { Component, OnInit } from '@angular/core';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:588:28)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:607:3
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

curiouswebsterSoftware Engineer

Author

Commented:
createScript (vm.js:80:10)

is failing in both
curiouswebsterSoftware Engineer

Author

Commented:
This seems like the same error, but I don't really get the solution.


https://github.com/nodejs/node/issues/12383

Do you?
Justin EstradaWordPress Developer

Commented:
Why are you writing JavaScript in Angular. Angular uses Typescript files and then converts them to JavaScript
curiouswebsterSoftware Engineer

Author

Commented:
Well, I am trying to validate my Angular installation works. On Windows, this simple js file hello.js worked in the browser at port 4200.

If you have a better idea as a way to validate it, please show me. The file you have me failed, just like my simple hello.js file failed.
Justin EstradaWordPress Developer

Commented:
ng new your-app-name
npm install
ng serve
then put your console log inside app.component.ts
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

	constructor () {
console.log("Angular works on the Mac");
       }

	ngOnInit( ) {
console.log("Angular works on the Mac");
	}

}

Open in new window

curiouswebsterSoftware Engineer

Author

Commented:
I did fail to call new on the last try, Thanks.

But it fails on "ng new myapp"

So, there is some installation error, it seems.

$ ng new myapp
module.js:529
    throw err;
    ^

Error: Cannot find module '@ngtools/json-schema'
    at Function.Module._resolveFilename (module.js:527:15)
    at Function.Module._load (module.js:476:23)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/@angular/cli/models/config/config.js:7:23)
    at Module._compile (module.js:624:30)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)

Open in new window

curiouswebsterSoftware Engineer

Author

Commented:
I think the problem is that I am running the command from the terminal window, not AngularCLI.

HOw do I launch that on a Mac?
curiouswebsterSoftware Engineer

Author

Commented:
I found an error that point to an installation problem:

ng --version
module.js:529
    throw err;
    ^

Error: Cannot find module '@ngtools/json-schema'
    at Function.Module._resolveFilename (module.js:527:15)
    at Function.Module._load (module.js:476:23)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/@angular/cli/models/config/config.js:7:23)
    at Module._compile (module.js:624:30)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
WordPress Developer
Commented:
terminal is perfect. Your in luck too, everything is better for terminals on Mac opposed to Windows
To make sure you have the latest Angular CLI installed

sudo npm install -g @angular/cli@latest

Then Angular Commands are run with ng
SO run ng -v to make sure you have Angular installed
Screen-Shot-2017-10-07-at-8.35.36-PM.png
curiouswebsterSoftware Engineer

Author

Commented:
I ran "sudo npm install -g @angular/cli@latest"

and was shown an update was available

UPdate
so I ran:

$sudo npm i -g npm

$sudo npm i -g npm
Password:
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
/usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
+ npm@5.4.2
added 21 packages, removed 78 packages and updated 19 packages in 8.601s

All looked good until here:
$ ng -v
module.js:529
    throw err;
    ^

Error: Cannot find module '@ngtools/json-schema'
    at Function.Module._resolveFilename (module.js:527:15)
    at Function.Module._load (module.js:476:23)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/@angular/cli/models/config/config.js:7:23)
    at Module._compile (module.js:624:30)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)

Do I need to uninstall Angular?
curiouswebsterSoftware Engineer

Author

Commented:
I uninstalled Angular, and even cleaned the cache, the re-installed. But its still failing. Is my Node compromised? It may have been installed with HomeBrew.

I used "sudo npm install -g @angular/cli@latest"

and still got permission errors.


gyp verb ensuring nodedir is created /usr/local/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp/8.6.0
gyp WARN EACCES user "root" does not have permission to access the dev dir "/usr/local/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp/8.6.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp"
gyp verb tmpdir == cwd automatically will remove dev files after to save disk space
gyp verb command install [ '8.6.0' ]
gyp verb install input version string "8.6.0"
gyp verb install installing version: 8.6.0
gyp verb install --ensure was passed, so won't reinstall if already installed
gyp verb install version not already installed, continuing with install 8.6.0
gyp verb ensuring nodedir is created /usr/local/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp/8.6.0
gyp WARN EACCES user "root" does not have permission to access the dev dir "/usr/local/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp/8.6.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp"
gyp verb tmpdir == cwd automatically will remove dev files after to save disk space
gyp verb command install [ '8.6.0' ]
gyp verb install input version string "8.6.0"
gyp verb install installing version: 8.6.0
gyp verb install --ensure was passed, so won't reinstall if already installed
gyp verb install version not already installed, continuing with install 8.6.0
gyp verb ensuring nodedir is created /usr/local/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp/8.6.0
gyp WARN EACCES user "root" does not have permission to access the dev dir "/usr/local/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp/8.6.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp"
gyp verb tmpdir == cwd automatically will remove dev files after to save disk space
gyp verb command install [ '8.6.0' ]
gyp verb install input version string "8.6.0"
gyp verb install installing version: 8.6.0
gyp verb install --ensure was passed, so won't reinstall if already installed
gyp verb install version not already installed, continuing with install 8.6.0
gyp verb ensuring nodedir is created /usr/local/lib/node_modules/@angular/cli/node_modules/node-sass/.node-gyp/8.6.0
Roberts-MacBook-Pro-2:angular RobertAntonellis$
curiouswebsterSoftware Engineer

Author

Commented:
thanks. You tried.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial