The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Fixing A JavaScript SyntaxError - Cannot use import statement outside a module Error

TODO: Pull subtitle into page object

Credit where it's due, these are the notes I took straight from this page by Kevin Leary

Node defaults CommonJS.

That means it uses `require` instead of `import`.

You'll need to switch to treating code as `ECMAScript modules` if you want to use `import`.

You can do that by:

1. Switching your file extension from `.js` to `.mjs` 2. Add `"type": "module"` to `package.json`, or 3. Run your script with `node --input-type module`

You can't use both `import` and `require`. You'll have to choose one or the other. (NOTE: I'm pretty sure there is a way to use both with a loader in at least React/Next if not vanialla)

More details on modules in node

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Fixing A JavaScript SyntaxError - Cannot use import statement outside a module Error

-- p

Credit where it's due, these are the notes I took straight from <<link|this page by Kevin Leary|https://www.kevinleary.net/js-fix-for-syntaxerror-cannot-use-import-statement-outside-a-module/>>

-- p

Node defaults CommonJS.

-- p

That means it uses `require` instead of `import`.

-- p

You'll need to switch to treating code as `ECMAScript modules` if you want to use `import`.

-- p

You can do that by:

-- p

1. Switching your file extension from `.js` to `.mjs`
2. Add `"type": "module"` to `package.json`, or
3. Run your script with `node --input-type module`

-- p

You can't use both `import` and `require`. You'll have to choose one or the other. (NOTE: I'm pretty sure there is a way to use both with a loader in at least React/Next if not vanialla)

-- p

<<link|More details on modules in node|https://nodejs.org/api/esm.html#/esm_enabling>>


-- categories
-- JavaScript

-- metadata
-- date: 2021-11-04 12:54:03
-- id: 20eoft5q
-- status: published
-- type: post
-- SCRUBBED_NEO: false
-- site: aws