3

I am attempting to run a file in my node environment on my macbook. I keep getting the following terminal error messages:

/.../filename.js: line 2: syntax error near unexpected token `('
/.../filename.js: line 2: `const fs = require('../build/index.js');'

code:

#!/usr/bin/env node
const fs = require('../build/index.js');

The command I'm giving is:

node index.js

(I know the file names are the same but its calling a different file.)

I can't seem to figure out why it is finding the extra `.

I have tried:

Little more background, this is a project I've picked up and this is my first time trying to run it.

EDIT: Code for index.js

'use strict';

var _child_process = require('child_process');

var _logger = require('./logger');

var _logger2 = _interopRequireDefault(_logger);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var l = new _logger2.default();

l.log('Starting backup');
var wd = __dirname.split('/');
var basePath = wd.slice(0, wd.length - 1).join('/');
var execPath = basePath + '/path/filename.js';
var stdout = (0, _child_process.execSync)('. ' + execPath + ' -a ' + basePath + '/something.json -b ' + basePath + '/backups');

l.log(stdout);
1
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat. Commented Apr 19, 2019 at 20:42

1 Answer 1

2

The issue is that the index.js calls filename.js through child_process.execSync() method, that's basically "execute this file using shell and give me its output".

Your filename.js has a shebang, and it would do intended (run using node), if not for this tiny dot in execSync() first parameter. It looks like this:

. /some/path/filename.js -a /some/path/something.json -b /some/path/backups

In sh-shells the . is a shorthand for source builtin command, that means "execute this file in shell and return its exit code". Thus, filename.js executes in shell, not in the node.

Just remove this dot (with space after it) and it will work.

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

1 Comment

Good news: You've solved this issue. Bad news: new error, cannot find module. Going to get to work on that now.

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.