ES2015 syntax error

Asked

Viewed 48 times

-3

I developed an npm package using es2015 preset in local environment worked very well because I was running Babel-Node index.js.

When I posted it on npm and installed in a project to test. Using it as follows:

import ValidateFlag from 'validate-flag';

it already returns an internal error in lib talking syntax pq I used import and export default to develop.

In this project I used to test I’m with the preset of the es2015

error in lib

C:\Users\joao\Desktop\ol\node_modules\validate-flag\main.js:54
export default Cartoes;
^^^^^^
SyntaxError: Unexpected token 'export'

And error tbm in Imports

Error while adding "type":"module"

C:\Users\joao\Desktop\ol>npm test

> [email protected] test
> babel-node index.js

node:internal/modules/cjs/loader:1119
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM] [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\joao\Desktop\ol\node_modules\validate-flag\main.js
require() of ES modules is not supported.
require() of C:\Users\joao\Desktop\ol\node_modules\validate-flag\main.js from C:\Users\joao\Desktop\ol\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename main.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\joao\Desktop\ol\node_modules\validate-flag\package.json.

    at new NodeError (node:internal/errors:278:15)
    at Module._extensions..js (node:internal/modules/cjs/loader:1119:13)
    at Object.require.extensions.<computed> [as .js] (C:\Users\joao\Desktop\ol\node_modules\babel-register\lib\node.js:152:7)
    at Module.load (node:internal/modules/cjs/loader:967:32)
    at Function.Module._load (node:internal/modules/cjs/loader:807:14)
    at Module.require (node:internal/modules/cjs/loader:991:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (C:/Users/joao/Desktop/ol/index.js:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1102:14)
    at loader (C:\Users\joao\Desktop\ol\node_modules\babel-register\lib\node.js:144:5) {
  code: 'ERR_REQUIRE_ESM'
}
npm ERR! code 1
npm ERR! path C:\Users\joao\Desktop\ol
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c babel-node index.js

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\joao\AppData\Local\npm-cache\_logs\2021-04-03T22_14_12_110Z-debug.log

C:\Users\joao\Desktop\ol>

This ol directory is where I am testing my lib as if I were a user

  • um erro interno na lib falando da syntax What I told you?

  • 1

    I edited the question, put the error there

2 answers

-3

The lib is for Nodejs or Javascript?

If you go to Ode try to use module.exports = Cartoes; instead of export default Cartões;

-4

C:\Users\joao\Desktop\ol\node_modules\validate-flag\main.js:54
export default Cartoes;
^^^^^^
SyntaxError: Unexpected token 'export'

This means that C:\Users\joao\Desktop\ol\node_modules\validate-flag\package.json line is missing "type": "module"

  • 1

    When adding the type: "type":"module" generated the following error, I will edit the question with the current error

Browser other questions tagged

You are not signed in. Login or sign up in order to post.