Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing Flags to Node.js #3060

Closed
MarkHerhold opened this issue Oct 7, 2017 · 18 comments
Closed

Passing Flags to Node.js #3060

MarkHerhold opened this issue Oct 7, 2017 · 18 comments

Comments

@MarkHerhold
Copy link

I can't figure out a way to pass the --expose-http2 flag to node. I would like to be able to do:

mocha test.js -- --expose-http2
OR
mocha test.js --node-flags 'expose-http2'

Is this currently possible (using mocha v3.2.0)?

@jondubois
Copy link

Also I need to pass an --experimental-modules flag to Node.js to support the new ES6 (.mjs) modules.

@ScottFreeCode
Copy link
Contributor

The flags Mocha will pass to Node are in bin/mocha (whereas Mocha's own flags are in bin/_mocha). To pass other flags to Node that Mocha does not support, you can node <node flags here> node_modules/mocha/bin/_mocha <mocha arguments here> instead of mocha ....

@MarkHerhold
Copy link
Author

Perfect. Thank you @ScottFreeCode 👍

@stefanwalther
Copy link

@jondubois Did you get it to run with --experimental-modules?

@jondubois
Copy link

jondubois commented Nov 21, 2017

@stefanwalther ScottFreeCode's solution above worked well for me. You just need to run the mocha script with the node command instead of directly through the mocha command. You can add an npm test script to your package.json to use the alternative approach instead of the mocha command.

@michael-brade
Copy link
Contributor

Well... I cannot get it to work with --experimental-modules because node complains:

$ node --experimental-modules node_modules/.bin/_mocha
Error [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension: node_modules/mocha/bin/_mocha
    at Loader.exports.resolve [as resolver] (internal/loader/ModuleRequest.js:126:13)
    at Loader.resolve (internal/loader/Loader.js:71:40)
    at Loader.getModuleJob (internal/loader/Loader.js:101:40)
    at Loader.import (internal/loader/Loader.js:128:28)

@seenickcode
Copy link

Did you get this to work @michael-brade ?

@michael-brade
Copy link
Contributor

@seenickcode nope, I gave up on it and won't use modules for the time being. Modules caused a whole lot of problems with the toolchain and I wasted way too much of my precious time on it. Maybe I'll try again in a year or two when it's ready.

@pukapukan
Copy link

you can use esm if you want to use modules. install esm then adding -r esm shoud do the trick.

@Rob-pw
Copy link

Rob-pw commented Jun 25, 2018

@pukapukan indeed it does!

@jmsv
Copy link

jmsv commented Nov 6, 2018

@pukapukan works perfectly, thanks!

I was initally confused where to add this (whether it was a node flag or a mocha flag) but my tests are working after the following package.json change:

 "scripts": {
-  "test": "mocha test.js"
+  "test": "mocha test.js -r esm"
 }

hope this helps anyone else slightly confused for a similar reason

@prescientmoon
Copy link

@pukapukan works perfectly, thanks!

I was initally confused where to add this (whether it was a node flag or a mocha flag) but my tests are working after the following package.json change:

 "scripts": {
-  "test": "mocha test.js"
+  "test": "mocha test.js -r esm"
 }

hope this helps anyone else slightly confused for a similar reason

ERROR: Cannot find module 'esm'

@jmsv
Copy link

jmsv commented Apr 23, 2019

ERROR: Cannot find module 'esm'

try npm install esm

@justinfagnani
Copy link

Has anyone gotten this to work with Node 12 and --experimental-modules?

My test script looks like:

    "test": "node --experimental-modules node_modules/mocha/bin/_mocha \"test/**/*_test.js\"",

But I'm still getting a syntax error on on import {} in my test modules. I have "type": "module" in my package.json too.

@fatso83
Copy link
Contributor

fatso83 commented Jun 27, 2019

@justinfagnani After experimenting with the experimental module support in Node and reading up on how the new support works, I believe testing ES Modules using Mocha alone cannot be made to work using just Node at the moment.

Using the built-in support isn't possible until Mocha decides it to be by changing its core. You can pass the flags to enable module support in Node like this: node --experimental-modules $(npm bin)/_mocha, but it will still crash on running code using import * from './my-module' with the following error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/fatso/dev/sinon/test/es2015/module-support-assessment-test.mjs

Wuuut? The reason Node complains is that Mocha imports the test files using the normal CommonJS require, while Node requires it to use import for it to enable the mode where files are running as ES Modules. That means Mocha must change for this to work. Until that happens, requiring the external esm module (simplest) or using transpilation (if you have no other way) is your answer.

cc: @stefanwalther

@brandonros
Copy link

mocha -r esm test/*.mjs
node --experimental-modules ./node_modules/mocha/bin/mocha test/*.mjs

both fail for me with Must use import to load ES Module

@fatso83
Copy link
Contributor

fatso83 commented Dec 13, 2019

@brandonros I linked to an article from the Node team on how the new module support works in Node 12. Excerpt:

Files ending in .mjs are explicitly treated as ES modules in import statements and when run via the node command.

That means you cannot expect files ending in .mjs to be treated in any other way, even if loading an additional library: Node will still use a different runtime behaviour for these files. You use the esm library to enable ES Module support when the Node runtime itself does not support it, meaning that if your files are not treated as ES Modules by Node itself, the esm module will make sure the syntax does not cause runtime errors when encountering syntax such as import and export.

The only change you need to do is change the file extension to something not explicitly treated by Node in any specific way, like .es6 or just .js. You can see that is what we are doing in Sinon when testing our ESM functionality.

achingbrain added a commit to ipfs/aegir that referenced this issue Oct 20, 2020
If we want to pass flags to node that mocha doesn't understand, we have to pass them to node and then invoke mocha as a script.

Refs: mochajs/mocha#3060
@francoatmega
Copy link

Has anyone gotten this to work with Node 12 and --experimental-modules?

My test script looks like:

    "test": "node --experimental-modules node_modules/mocha/bin/_mocha \"test/**/*_test.js\"",

But I'm still getting a syntax error on on import {} in my test modules. I have "type": "module" in my package.json too.

This worked for me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests