Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to create a simple npm package based on another called 'sidebar-v2' ( https://github.com/Turbo87/sidebar-v2 )

For that reason, I've tried to update the JS code to ES6, however when I try to test if the updates are ok, the mocha raises an exception, that I send below:

> @rmmariano/ol-sb@1.0.0-rc.1 test /home/inpe/Documents/datainfo/npm_packages/ol-sb
> mocha --require @babel/register
/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/ol/control.js:5
export {default as Attribution} from './control/Attribution.js';
^^^^^^
SyntaxError: Unexpected token export
    at new Script (vm.js:85:7)
    at createScript (vm.js:266:10)
    at Object.runInThisContext (vm.js:314:10)
    at Module._compile (internal/modules/cjs/loader.js:698:28)
    at Module._compile (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Object.newLoader [as .js] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Module.require (internal/modules/cjs/loader.js:667:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/inpe/Documents/datainfo/npm_packages/ol-sb/src/ol3-sidebar.js:1:1)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Module._compile (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Object.newLoader [as .js] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Module.require (internal/modules/cjs/loader.js:667:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/inpe/Documents/datainfo/npm_packages/ol-sb/src/index.js:1:1)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Module._compile (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Object.newLoader [as .js] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Module.require (internal/modules/cjs/loader.js:667:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/inpe/Documents/datainfo/npm_packages/ol-sb/test/test.js:4:1)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Module._compile (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Object.newLoader [as .js] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Module.require (internal/modules/cjs/loader.js:667:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at /home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/mocha.js:327:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/mocha.js:324:14)
    at Mocha.run (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/mocha.js:801:10)
    at Object.exports.singleRun (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/run-helpers.js:207:16)
    at exports.runMocha (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/run-helpers.js:300:13)
    at Object.exports.handler.argv [as handler] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/run.js:296:3)
    at Object.runCommand (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/lib/command.js:238:44)
    at Object.parseArgs [as _parseArgs] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/yargs.js:1089:24)
    at Object.parse (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/yargs.js:566:25)
    at Object.exports.main (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/cli.js:62:6)
    at Object.<anonymous> (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/bin/_mocha:10:23)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
    at internal/main/run_main_module.js:21:11
npm ERR! Test failed.  See above for more details.

I've already tried to follow the answer of other questions on StackOverflow, such as:

  • Babel unexpected token import when running mocha tests

  • Unexpected token import, can not set up ES6 in my JS project with Mocha, Chai and Sinon

  • But no one of them worked to me, unfortunately.

    When I try to update my .babelrc to:

    "presets": ["@babel/preset-env", "es2015"]

    The error changes to:

    > @rmmariano/ol-sb@1.0.0-rc.1 test /home/inpe/Documents/datainfo/npm_packages/ol-sb
    > mocha --require @babel/register
    /home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/yargs.js:1148
          else throw err
    Error: Cannot find module 'babel-preset-es2015' from '/home/inpe/Documents/datainfo/npm_packages/ol-sb'
        at Function.module.exports [as sync] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/resolve/lib/sync.js:58:15)
        at resolveStandardizedName (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/files/plugins.js:101:31)
        at resolvePreset (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/files/plugins.js:58:10)
        at loadPreset (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/files/plugins.js:77:20)
        at createDescriptor (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-descriptors.js:154:9)
        at items.map (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
        at Array.map (<anonymous>)
        at createDescriptors (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
        at createPresetDescriptors (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
        at presets (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-descriptors.js:47:19)
        at mergeChainOpts (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-chain.js:320:26)
        at /home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-chain.js:283:7
        at buildRootChain (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-chain.js:120:22)
        at loadPrivatePartialConfig (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/partial.js:85:55)
        at loadFullConfig (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/full.js:43:39)
        at loadOptions (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/index.js:27:36)
        at OptionManager.init (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/index.js:231:36)
        at compile (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/register/lib/node.js:61:42)
        at compileHook (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/register/lib/node.js:102:12)
        at Module._compile (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:93:29)
        at Module._extensions..js (internal/modules/cjs/loader.js:749:10)
        at Object.newLoader [as .js] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:104:7)
        at Module.load (internal/modules/cjs/loader.js:630:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
        at Function.Module._load (internal/modules/cjs/loader.js:562:3)
        at Module.require (internal/modules/cjs/loader.js:667:17)
        at require (internal/modules/cjs/helpers.js:20:18)
        at /home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/mocha.js:327:36
        at Array.forEach (<anonymous>)
        at Mocha.loadFiles (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/mocha.js:324:14)
        at Mocha.run (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/mocha.js:801:10)
        at Object.exports.singleRun (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/run-helpers.js:207:16)
        at exports.runMocha (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/run-helpers.js:300:13)
        at Object.exports.handler.argv [as handler] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/run.js:296:3)
        at Object.runCommand (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/lib/command.js:238:44)
        at Object.parseArgs [as _parseArgs] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/yargs.js:1089:24)
        at Object.parse (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/yargs.js:566:25)
        at Object.exports.main (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/cli.js:62:6)
        at Object.<anonymous> (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/bin/_mocha:10:23)
        at Module._compile (internal/modules/cjs/loader.js:738:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:749:10)
        at Module.load (internal/modules/cjs/loader.js:630:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
        at Function.Module._load (internal/modules/cjs/loader.js:562:3)
        at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
        at internal/main/run_main_module.js:21:11
    npm ERR! Test failed.  See above for more details.
    

    package.json

    "scripts": { "start": "nodemon --exec babel-node ./src/index.js", "test": "./node_modules/.bin/mocha --require @babel/register" "devDependencies": { "@babel/cli": "^7.2.3", "@babel/core": "^7.3.4", "@babel/node": "^7.2.2", "@babel/preset-env": "^7.3.4", "@babel/register": "^7.0.0", "chai": "^4.2.0", "mocha": "^6.0.2", "nodemon": "^1.18.10" "dependencies": { "ol": "^5.3.1"

    .babelrc

    "presets": ["@babel/preset-env"]

    Could someone help me?

    Source code can be found here: https://github.com/rmmariano/ol-sb

    Edit 1:

    I've tried to update my .babelrc to:

    "presets": ["@babel/preset-env", "es2015"]

    And to install the package:

    npm install -S babel-preset-es2015
    

    But the following error starts to appear when I run "npm test":

    > @rmmariano/ol-sb@1.0.0-rc.1 test /home/inpe/Documents/datainfo/npm_packages/ol-sb
    > mocha --require @babel/register
    /home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/yargs.js:1148
          else throw err
    Error: Plugin/Preset files are not allowed to export objects, only functions. In /home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/babel-preset-es2015/lib/index.js
        at createDescriptor (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-descriptors.js:178:11)
        at items.map (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
        at Array.map (<anonymous>)
        at createDescriptors (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
        at createPresetDescriptors (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
        at presets (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-descriptors.js:47:19)
        at mergeChainOpts (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-chain.js:320:26)
        at /home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-chain.js:283:7
        at buildRootChain (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/config-chain.js:120:22)
        at loadPrivatePartialConfig (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/partial.js:85:55)
        at loadFullConfig (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/full.js:43:39)
        at loadOptions (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/config/index.js:27:36)
        at OptionManager.init (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/core/lib/index.js:231:36)
        at compile (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/register/lib/node.js:61:42)
        at compileHook (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/@babel/register/lib/node.js:102:12)
        at Module._compile (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:93:29)
        at Module._extensions..js (internal/modules/cjs/loader.js:749:10)
        at Object.newLoader [as .js] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/pirates/lib/index.js:104:7)
        at Module.load (internal/modules/cjs/loader.js:630:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
        at Function.Module._load (internal/modules/cjs/loader.js:562:3)
        at Module.require (internal/modules/cjs/loader.js:667:17)
        at require (internal/modules/cjs/helpers.js:20:18)
        at /home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/mocha.js:327:36
        at Array.forEach (<anonymous>)
        at Mocha.loadFiles (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/mocha.js:324:14)
        at Mocha.run (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/mocha.js:801:10)
        at Object.exports.singleRun (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/run-helpers.js:207:16)
        at exports.runMocha (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/run-helpers.js:300:13)
        at Object.exports.handler.argv [as handler] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/run.js:296:3)
        at Object.runCommand (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/lib/command.js:238:44)
        at Object.parseArgs [as _parseArgs] (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/yargs.js:1089:24)
        at Object.parse (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/yargs.js:566:25)
        at Object.exports.main (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/lib/cli/cli.js:62:6)
        at Object.<anonymous> (/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/mocha/bin/_mocha:10:23)
        at Module._compile (internal/modules/cjs/loader.js:738:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:749:10)
        at Module.load (internal/modules/cjs/loader.js:630:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
        at Function.Module._load (internal/modules/cjs/loader.js:562:3)
        at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
        at internal/main/run_main_module.js:21:11
    npm ERR! Test failed.  See above for more details.
    

    Edit 2:

    I've tried to update my .babelrc to:

    "presets": ["@babel/preset-env", "@babel/preset-es2015"]

    And to install the package:

    npm install -S @babel/preset-es2015
    

    But the following error starts to appear when I run "npm test":

    > @rmmariano/ol-sb@1.0.0-rc.1 test /home/inpe/Documents/datainfo/npm_packages/ol-sb
    > mocha --require @babel/register
    /home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/yargs.js:1148
          else throw err
    Error: Cannot find module '@babel/preset-es2015' from '/home/inpe/Documents/datainfo/npm_packages/ol-sb'
                    Use the preset "presets": ["@babel/preset-env", "es2015"], and install the plugin with npm install -S babel-preset-es2015
    – Seblor
                    Mar 14, 2019 at 15:00
                    I've tried to follow your tips, but the following error started to appear: "/home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/yargs/yargs.js:1148       else throw err            ^  Error: Plugin/Preset files are not allowed to export objects, only functions. In /home/inpe/Documents/datainfo/npm_packages/ol-sb/node_modules/babel-preset-es2015/lib/index.js"  Do you have any idea?
    – rmmariano
                    Mar 14, 2019 at 16:30
                    You might need to replace es2015 in your preset by @babel/preset-es2015 and install babel/preset-es2015 instead of babel-preset-es2015.
    – Seblor
                    Mar 14, 2019 at 18:08
                    I've tried to do these steps as well, but unfortunately another error raised:  "Error: Cannot find module '@babel/preset-es2015' from '/home/inpe/Documents/datainfo/npm_packages/ol-sb" I edited the question describing what I did.
    – rmmariano
                    Mar 14, 2019 at 18:27
                    Having the same issue with      "@babel/cli": "^7.4.3",     "@babel/core": "^7.4.3",     "@babel/node": "^7.2.2",     "@babel/preset-env": "^7.4.3",     "@babel/register": "^7.4.0",     "chai": "^4.2.0",     "mocha": "^6.0.2"
    – ricick
                    Apr 4, 2019 at 2:00
    

    Short solution. I had a similar problem with imports and exports.

    Also, Babel did not work well with mocha version 7.2.0 and npm version 6.14.4. So I solved the problem using esm version 3.2.25.

    npm install --save-dev esm 
    

    My package.json is using this:

    "scripts": {
      "test": "mocha -r esm --exit"
                    This worked when I downgraded to mocha 8.4.0; the error comes back if I went back to mocha 9.1.3.  (node 16, esm 3.2.25). We've a sprawling mix of require and import style modules, so fixing mocha at 8.4.0 seems like the easiest thing to do for now.
    – Darren Cook
                    Dec 10, 2021 at 10:05
                    Didn't work for me. Getting same "unexpected token 'export'" message. Still getting error for mocha versions 8.4.0, 9.1.3, 9.2.0.
    – thund
                    Jan 27, 2022 at 18:55
    

    I was searching through the whole internet to find a solution. I came up with this:

    npx mocha --require ts-node/register --require esm src/**/*.spec.ts
    

    Or in package.json scripts (both options work, choose whichever you like):

    "scripts": {
        "test": "mocha -r esm -r ts-node/register src/**/*.spec.ts",
        "test-ts": "ts-mocha -r esm -p tsconfig.json src/**/*.spec.ts"
    

    And don't forget to add esm to devDependencies:

    npm install --save-dev esm
    

    so in package.json it is gonna be

    "devDependencies": {
        "esm": "^3.2.25"
                    But I installed the "ol" through the npm: "npm install ol". So should not nodejs find it? This dependency is on my package.json and node_modules.
    – rmmariano
                    Mar 14, 2019 at 18:30
    

    I had the same problem. And managed to solve it after re-installing windows and it still not working.

    For me it was that the project folder was on a different drive accessed via a symbolic link. I have a small SSD for boot so keep project folders on a second larger disk drive, and symlink from my home directory to this folder.

    If i navigate directly to the folder it works.

    I'll report this to both Mocha and Babel teams. I'm not sure who's problem this is.

    to see the modifications that the package babel-upgrade will do without applying them, and if your are satisfied:

    npx babel-upgrade --write
    

    to apply the modifications.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.