question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocha, Babel, and Windows symlinks

See original GitHub issue

Bug Report

Current Behavior Mocha is not running Babel beforehand when the project is behind a symlink on windows.

I have a small SSD and a larger HDD so to make my life easier I have a hard symlink to D:/projects in C:/Users/MyUser home directory, so can access via ~/projects.

When running mocha --require @babel/register I’m getting the Unexpected identifier import problem. I even re-installed windows thinking it might be an environment problem before figuring out that this was the problem

Input Code Create a simple mocha es6 project in realfoldercontainer/myproject, to be run via npm test with mocha -r @babel/register in package.json

Create a hard symlink with mklink /j linkname realfoldercontainer

Navigate to linkname/myproject

Run npm test

Expected behavior/code

npm test

> mochatest@1.0.0 test D:\projects\mochatest
> mocha -r @babel/register

  index test
    sayHello function
      √ should say Hello guys!


  1 passing (44ms)

Actual behavior

npm test

> mochatest@1.0.0 test D:\testlink\mochatest
> mocha -r @babel/register

D:\projects\mochatest\test\index.test.js:3
import sayHello from "../src/index"
       ^^^^^^^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:749:23)
    at Module._compile (D:\projects\mochatest\node_modules\pirates\lib\index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:816:10)
    at Object.newLoader [as .js] (D:\projects\mochatest\node_modules\pirates\lib\index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:672:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:612:12)
    at Function.Module._load (internal/modules/cjs/loader.js:604:3)
    at Module.require (internal/modules/cjs/loader.js:711:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at D:\projects\mochatest\node_modules\mocha\lib\mocha.js:327:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (D:\projects\mochatest\node_modules\mocha\lib\mocha.js:324:14)
    at Mocha.run (D:\projects\mochatest\node_modules\mocha\lib\mocha.js:801:10)
    at Object.exports.singleRun (D:\projects\mochatest\node_modules\mocha\lib\cli\run-helpers.js:207:16)
    at exports.runMocha (D:\projects\mochatest\node_modules\mocha\lib\cli\run-helpers.js:300:13)
    at Object.exports.handler.argv [as handler] (D:\projects\mochatest\node_modules\mocha\lib\cli\run.js:296:3)
    at Object.runCommand (D:\projects\mochatest\node_modules\yargs\lib\command.js:238:44)
    at Object.parseArgs [as _parseArgs] (D:\projects\mochatest\node_modules\yargs\yargs.js:1089:24)
    at Object.parse (D:\projects\mochatest\node_modules\yargs\yargs.js:566:25)
    at Object.exports.main (D:\projects\mochatest\node_modules\mocha\lib\cli\cli.js:62:6)
    at Object.<anonymous> (D:\projects\mochatest\node_modules\mocha\bin\_mocha:10:23)
    at Module._compile (internal/modules/cjs/loader.js:805:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:816:10)
    at Module.load (internal/modules/cjs/loader.js:672:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:612:12)
    at Function.Module._load (internal/modules/cjs/loader.js:604:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:868:12)
    at internal/main/run_main_module.js:21:11
npm ERR! Test failed.  See above for more details.

Babel Configuration (.babelrc, package.json, cli command)

{
  "your": { "config": "here" }
}

Environment node v11.13.0

@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”, “jest”: “^24.7.1”, “mocha”: “^6.0.2”

Additional context/Screenshots I can now corretctly run the tests by going directly to the folder, but it took a long time to figure this out, including a system rebuild to try and fix the problem.

I’m unsure as to whether this is a babel or mocha issue, so will be posting in both github issues pages.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
loganfsmythcommented, May 4, 2019

The issue here is that @babel/register, by default, only runs Babel on files inside the working directory and not inside a node_modules folder. If you want a broader scope, you’ll need to configure it. For example you can do --require ./babel-register.js and make that file:

// babel-register.js

require("@babel/register")({
  ignore: [/node_modules/]
});

to compile all non-node_modules files, even those outside the working directory.

0reactions
loganfsmythcommented, May 5, 2019

Does that mean that Babel considers symlinked win10 directory different from the original source directory and thus being “outside” working directory?

Yes exactly.

Maybe there’s some other problem in play?

Yes I realize that I should have mentioned one other thing. .babelrc config files a) only apply to files in child directories b) need to be in a folder Babel expects .babelrc configs to be in.

Like the other case, since Babel sees the file as outside of your project, there is no way for a .babelrc to apply to it. This is why Babel 7 added support for project-wide/file-path-independent configuration that can be loaded from a babel.config.js. you can read more at https://babeljs.io/docs/en/config-files#project-wide-configuration

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocha 6, Babel 7, ES6: SyntaxError: Unexpected token export
I have a small SSD for boot so keep project folders on a second larger disk drive, and symlink from my home directory...
Read more >
Config Files - Babel.js
New in Babel 7.x, Babel has a concept of a "root" directory, which defaults to the current working directory. For project-wide configuration, Babel...
Read more >
mochajs/contributors - Gitter
I use the windows console to run Linux tmux, which can run a windows power shell core ... Our backers support mocha and...
Read more >
Running Babel on NodeJS5.12.0 image inside the Docker for ...
It looks like that Symlinks on shared volumes are not supported in Docker for Windows. ... Had/have the same issue, symlinks isn't supported,...
Read more >
Build Performance - webpack
Set resolve.symlinks: false if you don't use symlinks (e.g. npm link or yarn link ). Set resolve. ... Babel. Minimize the number of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found