Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' #55

Closed
tmanderson opened this issue Aug 15, 2017 · 11 comments
Labels

Comments

@tmanderson
Copy link

I'm seeing this error:

`SyntaxError: 'import' and 'export' may appear only with 'sourceType: module'`

Sorry if this is completely my mistake, but I can't seem to find anything in the README referring to the sourceType: module setting?

My package.json contents:

"engines": {
  "node": "8.1.2"
},
"dependencies": {
  "@std/esm": "^0.3.0",
  "chalk": "^2.1.0"
},
...
"esm": {
  "esm": "all",
  "cjs": true
}

I'm running my script with the following command:

$> node -r @std/esm src/index.js

Where index.js contains:

'use module';
import chalk from 'chalk';

I've also tried import chalk from './node_modules/chalk and ./node_modules/chalk/index with no luck.

I have verified that chalk is installed. Thanks!

@jdalton
Copy link
Member

jdalton commented Aug 15, 2017

Hi @tmanderson!

That's a parse error from acorn. I'm unable to repro in similating your setup.
Would you mind creating a simple repro repo so that I can take a look?

@tmanderson
Copy link
Author

Heya, @jdaltonsure can!

@jdalton
Copy link
Member

jdalton commented Aug 15, 2017

Thanks @tmanderson!

You've got the options specified incorrectly.
It should be the key "@std/esm": { "esm":"all", "cjs":true }. Your path to chalk can be restored to just "chalk" after that. Also since you've specified "all" for your esm support the "use module" directive isn't required. 😋

@jdalton jdalton closed this as completed Aug 15, 2017
@tmanderson
Copy link
Author

@jdalton, ah! I gotcha. Had a feeling it might be something like that.

Thanks for the quick response!

@jdalton
Copy link
Member

jdalton commented Aug 15, 2017

BTW I updated the parser error message to avoid confusion with @std/esm options.

@tmanderson
Copy link
Author

Ah, very nice. I'm hoping that'll save someone from the same mistake!

Thanks again.

@theKashey
Copy link

@jdalton - you should clearly indicate in README that one has to include additional information into the packages.json, or "this magic" will not work.
As I think - most if people will try to run this with *.js file, not mjs.
In ideal - add the ability to execute anything in all mode.

@jdalton
Copy link
Member

jdalton commented Aug 19, 2017

@theKashey I do

@theKashey
Copy link

Yep, but this is not very clear :(

Will NOT work out of the box for JS files. See below.

As for me - I have read the medium article, tried to run scripts, checked readme.. And understand my problem only here.

@jdalton
Copy link
Member

jdalton commented Aug 19, 2017

@theKashey Would you be up for creating a PR to update the readme?

@theKashey
Copy link

Will do my best.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants