Using NPM with meteor via Import

I’m trying to use CasperJS from NPM in my app. I’m following what I take to be the latest advice here:
http://guide.meteor.com/using-packages.html

I installed CasperJS using:
meteor npm install --save casperjs

Then I include it using
import casperjs from 'casperjs';

And I get the error:

app/server/parsers/casper.js:7
(STDERR) import casperjs from ‘casperjs’;
SyntaxError: Unexpected reserved word

What am I doing wrong?

What Meteor version are you using? It seems you are in an environment that doesn’t support modules (yet).

It’s meteor 1.3.

I did meteor update, so I assume it’s the latest version?

I’m absolutely certain of the version:

meteor --version

Meteor 1.3.1

is there anything else I can do?

Can you show the contents of your packages file and the first lines of app/server/parsers/casper.js?

/localnets/packages$ ls -alh

total 24K
drwxrwxr-x 6 jimmy jimmy 4.0K Apr 13 16:39 .
drwxrwxr-x 14 jimmy jimmy 4.0K Apr 14 17:14 …
drwxrwxr-x 6 jimmy jimmy 4.0K Jul 16 2015 accounts-entry
drwxrwxr-x 3 jimmy jimmy 4.0K Jul 16 2015 meteor-jasmine
drwxrwxr-x 8 jimmy jimmy 4.0K Jul 16 2015 meteor-jasmine-obs
drwxrwxr-x 2 jimmy jimmy 4.0K Jul 16 2015 tests-proxy

I’m not totally certain why the error in the previous post is in parsers/casper.js. The line of code is in a file called
server/parsers/photo_tag.js, and the file looks like this:

Meteor.methods({
    photo_tag:function(source_id, parameter, project_id){ 

	var source_type = 'photo_tag';

        import casperjs from 'casperjs';

    	var scrape_id = parser.logging.start(source_id, source_type, parameter);
    	console.log('PHOTO TAG CALLED: ', parameter);
});

Causing the error

app/server/parsers/photo_tag.js:7
W20160415-11:20:43.514(1)? (STDERR) import casperjs from ‘casperjs’;

CasperJS won’t work with require, which means it won’t work with Meteor’s ES2015 module support. From http://docs.casperjs.org/en/latest/installation.html:

While CasperJS is installable via npm, it is not a NodeJS module and will not work with NodeJS out of the box. You cannot load casper by using require(‘casperjs’) in node. Note that CasperJS is not capable of using a vast majority of NodeJS modules out there. Experiment and use your best judgement.

Sorry, you said packages file, not folder:

# Meteor packages used by this project, one per line.
#
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.

less
accounts-ui
accounts-password
momentjs:moment
aldeed:autoform
cunneen:mailgun
aldeed:collection2
mrt:flash-messages
d3
mrt:googlemaps
mizzao:autocomplete
aldeed:geocoder
matteodem:easy-search

iron:router
random
reactive-var
reactive-dict
twbs:bootstrap
meteorhacks:async
ostrio:neo4jdriver
zimme:iron-router-active
percolate:synced-cron
rzymek:randomcolor
http																														
tsega:bootstrap3-datetimepicker
useraccounts:bootstrap
d3js:d3
erasaur:meteor-lodash
alanning:roles
meteor-base
mobile-experience
mongo
blaze-html-templates
session
jquery
tracker
logging
reload
ejson
spacebars
check
standard-minifier-css
standard-minifier-js
aldeed:tabular
krstffr:instagram-fetcher

OK - thanks - what a nightmare. I should have read that.

Was so confused about the module system in Meteor that I assumed that was where the problem was.

1 Like

There is another problem with your code: imports need to be at the top of the file and can’t be within anything else, like in a Meteor.method.

OK, so now I’m trying to use PhantomJS directly, and I sill have the same problem. I’ve done

meteor npm install --save phantomjs-wrapper

and I see
W20160415-14:17:30.542(1)? (STDERR) app/server/parsers/photo_tag.js:1
W20160415-14:17:30.542(1)? (STDERR) (function(Npm,Assets){(function(){import phantomjs-wrapper from 'phantomjs-wra

import phantomjs-wrapper from 'phantomjs-wrapper';

Is the very first thing in my method file. It seems that I have Meteor 1.3, but that it doesn’t support modules?

Try making your import:

import phantomjsWrapper from 'phantomjs-wrapper';

This works for me along with using/calling the sample code posted on the phantomjs-wrapper npm page.

I still get

W20160415-16:16:34.342(1)? (STDERR) app/server/parsers/photo_tag.js:1
W20160415-16:16:34.342(1)? (STDERR) (function(Npm,Assets){(function(){import phantomjsWrapper from 'phantomjs-wrap
W20160415-16:16:34.342(1)? (STDERR) ^^^^^^
W20160415-16:16:34.345(1)? (STDERR) SyntaxError: Unexpected reserved word

This was recently upgraded from Meteor 0.9, could this be some kind of legacy thing? It was using meteorhacks/npm before.

This looks to me as that import statement is not at the very beginning of the file. It’s still in a function. Unless the Babel compiler is wrecking the thing. Not sure… :frowning:

You don’t have ecmascript in your package list. Try meteor add ecmascript.

OK, that sound promising, and it works for one of my projects.

However, for another the app just hangs when it gets to

jimmytidey@jimmytideysmbp2~/projects/localnets: meteor
[[[[[ ~/projects/localnets ]]]]]

=> Started proxy.
=> Started MongoDB.
Building for web.browser /

I’m not using the import phrase at the moment, so I guess I’ll just wait and see if subsequent updates fix this.

It may be worth doing

METEOR_DEBUG_BUILD=1 meteor

To see where it’s spending its time.

I guess it isn’t a mega surprise:

 DONE 2 running registerCompiler callback in package templating took 1

DONE 1 determining active plugins took 2151
START 1 determining active plugins
DONE 1 determining active plugins took 0
START 1 building for web.browser |
START 2 linking the program |
DONE 2 linking the program took 50 |
START 2 loading plugin compileCoffeescript from package coffeescript
DONE 2 loading plugin compileCoffeescript from package coffeescript took 591
START 2 running registerCompiler callback in package coffeescript
DONE 2 running registerCompiler callback in package coffeescript took 1
START 2 processing files with ecmascript (for target web.browser)

The fact it goes quiet at the ecmascript phase seems to suggest that there are a lot of files (or big files) that it’s working through. Is it possible you have some something(s) in your project folders that shouldn’t be there?

Thanks for all your help with this. Noticed that a folder in node_modules was 18 meg (tldjs). Reinstalled it and it came out at 188k. Now it all runs fine.

1 Like