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

SyntaxError: Unexpected token import #93

Closed
seangwright opened this issue Sep 12, 2017 · 2 comments
Closed

SyntaxError: Unexpected token import #93

seangwright opened this issue Sep 12, 2017 · 2 comments
Labels

Comments

@seangwright
Copy link

The error I see is for the first import statement in my require'd file that uses ES modules

node main.js

(function (exports, require, module, __filename, __dirname) { import * as cheerio from 'cheerio';

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:537:28)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)
/// main.js
require('@std/esm')(module, { cjs: true, esm: 'js' });
const build = require('./build');
build();
/// build.js
import * as cheerio from 'cheerio';
// ... some other stuff

export function doBuild() {
}

I must be using esm incorrectly?

My aim in using ES modules in node (and thus esm) is to either use tsc to produce ES module .js files from Typescript sources or us ts-node to run everything in memory.

Thanks!

@jdalton
Copy link
Member

jdalton commented Sep 12, 2017

Hi @sgwatgit!

require('@std/esm')(module, { cjs: true, esm: 'js' });

should be

require = require('@std/esm')(module, { cjs: true, esm: 'js' });

@jdalton jdalton closed this as completed Sep 12, 2017
@purtuga
Copy link

purtuga commented Sep 17, 2017

Hi @jdalton - first: THANK YOU for sharing this great tool.
I too was having issues setting up a small project to use this for my tests (using Tape). This post helped. May I suggest that you use this example in the docs? It was not obvious (until you pointed out here) that I needed to override require locally with what is returned by std/esm :)

/Paul

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