Unexpected token import when trying to use pm2 on my react app digitalocean droplet - node.js

I am trying to host my app on DigitalOcean and getting it running with pm2 but run into this error:
/home/bcavenagh/social/src/index.js:1
(function (exports, require, module, __filename, __dirname) { import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:27:21)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
I can get everything to run when using npm start but it doesnt work for pm2. In npm I am running from the ~/social folder but for pm2 I cd into ~/social/src and run:
pm2 start index.js
How do I get past this error?

You must compile/build your application, if not done so.
It will create a build folder (assuming you are using default configs with create-react-app), then you must point pm2 to the appropriate file (bundle.js or whatever you have) in that folder.
PM2 also supports running npm start command now. Checkout this question. What i usually do is, write a sh file, which updates code, does npm install and then at last runs pm2 command, for all deployments, which is similar to this answer in the previous link.

Related

Erorr running node.js project after moving to a new development machine: [!] SyntaxError: Unexpected token { in rollup.config.js

I have a working node.js development environment created with Anaconda. When I have moved the project to another machine, re-created identical environment, I get error when running npm run build (or npm run dev):
[!] SyntaxError: Unexpected token {
c:\test\myproject\rollup.config.js:1
(function (exports, require, module, __filename, __dirname) { import { nodeResolve } from '#rollup/plugin-node-resolve'
^
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:656:28)
at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Object.require.extensions.(anonymous function) [as .js] (c:\test\myproject\node_modules\rollup\dist\shared\loadConfigFile.js:560:13)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Module.require (internal/modules/cjs/loader.js:636:17)
This is the top line of my rollup.config.js where the error occurs:
import { nodeResolve } from '#rollup/plugin-node-resolve'
Node version: v10.13.0.
I have compared npm modules installed on both machines, they are identical, with identical versions. I'm truly puzzled. I'll be grateful for any pointers where to look for differences between the two machines and how to make the project working. I've already spent half a day looking at it and I've run out of ideas.
UPDATE:
I copied the whole conda environment to the new machine to mirror the working configuration on the old machine but the error still persists. So now I have identical environments, identical source code but it works on one machine and it doesn't on another.
It turns out rollup doesn't work properly when the project path goes through an NTFS directory junction.
Sample layout:
C:\Home - directory
\Work - directory
\MyProject - directory
C:\Work - junction to C:\Home\Work
Results:
cd C:\Work\MyProject
npm run build
.... results in error
cd C:\Home\Work\MyProject
npm run build
.... everything works OK

Composer Rest Server error "async *__getField() {"

I have followed this (https://medium.com/coinmonks/build-a-insurance-application-with-hyperledger-composer-and-react-js-part-1-3ebe7ad54986) tutorial upto "create our REST API".
Now when i run following command
composer-rest-server -c admin#secure-phi -n never -u true -w true
I get following error
/home/ali/.nvm/versions/node/v8.17.0/lib/node_modules/composer-rest-server/node_modules/formdata-node/lib/FormData.js:206
async *__getField() {
^
SyntaxError: Unexpected token *
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object. (/home/ali/.nvm/versions/node/v8.17.0/lib/node_modules/composer-rest-server/node_modules/#tim-lai/isomorphic-form-data/lib/index.js:2:18)
Is this a syntax related error or something else, and any way to fix this?
I found the error is related to generator not supported below node v8, but hyper-ledger-composer specifically requires node v8, so any work around to this?
It seems to be a version conflict between sub-dependencies in the graph.
Looks like the package 'g11n-pipeline' is requiring a different version of 'swagger-client' that pulls the dependency '#tim-lai/isomorphic-form-data'.
I did this workaround as it is a global package (we don't have access to the package-lock.json/package.json).
Since you've installed with NPM (npm i -g composer-rest-server):
cd /home/ali/.nvm/versions/node/v8.17.0/lib/node_modules/composer-rest-server
npm install swagger-client#3.9.0
rm -rf node_modules/g11n-pipeline/node_modules/swagger-client

Run native Nodejs in AS400 / iSeries

I'm trying to run Node.js on AS/400 server in order to use Node.js toolkit for IBM i.
I have installed Node v6.14.4 and NPM 3.10.10 and I'm following this documentation to test node.js: https://developer.ibm.com/articles/i-native-js-app-ibmi-with-nodejs/#access-ibm-i-native-objects
I created the sample javascript file and when I run (from PASE terminal), I get this error:
node /home/njs/sample.js
/sample.js:1
(function (exports, require, module, __filename, __dirname) { #����ï¿
½ï¿½ï¿½K���M È ï¿½ï¿½#����� ]^
SyntaxError: Invalid or unexpected token
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.runMain (module.js:611:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:160:9)
I supposed there is a problem with npm and I tried with a very simple js file (an only one console.log) and I get the same error.
I have solved my issue. It was because an enconding problem.
I was creating javascript files with EDTF on AS/400 environment and that was a problem. I created a new file with notepad and upload by Navigator for i and works properly.

PM2 - SyntaxError: Block-scoped declarations not yet supported outside strict mode

I'm currently installing a NodeJS service on an Ubuntu server. The application is written in coffeescript and runs fine on it's own. This is how I setup the project:
cd ~/test-project
nvm use v4.9.1
npm install
pm2 start index.js
However when I try to start it using pm2 i get the error below:
/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/node.js:132
let val = process.env[key];
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/index.js:9:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
And after doing some testing it seems that this error occurs no matter what script I start with pm2 so there must be something that isn't configured correctly..
I don't know if it matters but the script I am trying to run needs node v4.9.1 so I am using NVM to use that version, it seems to me that it is trying to run pm2 with that same node version somehow?
Any input is greatly appreciated as I've been stuck on this for way too long!!
I had the same issue on node version 4.x,
Downgraded the pm2 and it worked.
npm uninstall -g pm2
npm install -g pm2#3.0.4
'use strict';
Add above line of code to first line of the script file. let is used when you want a variable to be scoped to a block which is supported in strict mode in node v4.
See the documentation for more details.
#Vishnu Sing this is the complete output. The index.js file looks like this:
console.log('Hello World');
ubuntu#ip-172-31-32-5:~/test-project$ nano index.js
ubuntu#ip-172-31-32-5:~/test-project$ node -v
v10.15.3
ubuntu#ip-172-31-32-5:~/test-project$ sudo pm2 start index.js
/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/node.js:132
let val = process.env[key];
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/index.js:9:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
ubuntu#ip-172-31-32-5:~/test-project$

I have just the Started the learning the Type Script ans getting the below error.please help on this and what could be the mistake?

var n:string = "mary";
D:\Angular\TS>node Untitled_2.ts
D:\Angular\TS\Untitled_2.ts:1(function (exports, require, module,
__filename, __dirname) { var n:string = "mary";
^
SyntaxError: Unexpected token :
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:607:28)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
Your problem is that you have to compile Typescript code before you can run it with node. You can do this two ways.
Run tsc Untitled_2.ts and then run node Untitled_2.js (assuming no extra config that changes output paths)
Install ts-node and run ts-node Untitled_2.ts
You may need to prefix the above commands with npm run or yarn to add the command to your path.

Resources