Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing files without adding .js extension existing project

I am trying to try to add some BDD tests to this repo: https://github.com/rocket-pool/rocketpool

I've created a feature file in /test/features and a new steps file in /test/support.

The feature file looks like this:

Feature: Test

Scenario: Test Deposit Below The Minimum Amount
 When A Staker Tries to Deposit Below the Minimum Amount
 Then The Transaction Should Revert

The steps file complains about all the imports, which look like this :

import { printTitle } from '../_utils/formatting';

They do not include the .js extension. In order to get to this point (which I had to fight with using import vs require), I had to add "type": "module" to the package.json file, my run command looks like this npx @cucumber/cucumber test/features/test.feature --import test/support/*.js

If I add the .js extension to the imports in my steps file, it will then complain about all the imports in other files in the project. I do not want to change all of them to use .js, and I would like to just be able to be able to keep the changes to a minimum. I have looked into webpack and babel, but there are several config files included in the node_modules for these packages so I am not sure which ones to modify, or even if that will help me in this case. It appears I am in commonjs/esm hell and I have very little experience with JavaScript.

Edit: Tried this:

node --experimental-specifier-resolution=node npx @cucumber/cucumber test/features/*.feature --import test/support/*.js

Now get this error

Running command: node --experimental-specifier-resolution=node npx @cucumber/cucumber test/features/.feature --import test/support/.js --name "Test Deposit Below The Minimum Amount" (node:7504) ExperimentalWarning: The Node.js specifier resolution flag is experimental. It could change or be removed at any time. (Use node --trace-warnings ... to show where the warning was created) node:internal/errors:490 ErrorCaptureStackTrace(err); ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/c:/Users/_/git/rocketpool/npx' imported from c:\Users_\git\rocketpool
at new NodeError (node:internal/errors:399:5) at finalizeResolution (node:internal/modules/esm/resolve:308:15) at moduleResolve (node:internal/modules/esm/resolve:945:10) at defaultResolve (node:internal/modules/esm/resolve:1153:11) at nextResolve (node:internal/modules/esm/loader:163:28) at ESMLoader.resolve (node:internal/modules/esm/loader:838:30) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18) at ESMLoader.import (node:internal/modules/esm/loader:525:22) at node:internal/modules/run_main:58:28 at loadESM (node:internal/process/esm_loader:91:11) { code: 'ERR_MODULE_NOT_FOUND }

Node.js v18.16.0

ERROR: Command exited with status code 1.`

like image 637
user1984300 Avatar asked Sep 17 '25 21:09

user1984300


1 Answers

This exception Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/c:/Users/_/git/rocketpool/npx' is being returned, because the npx command is being interpreted as a module name, hence the ERR_MODULE_NOT_FOUND.

NODE_OPTIONS

Set the NODE_OPTIONS environment variable with the --experimental-specifier-resolution=node flag to enable the experimental specifier resolution feature

export NODE_OPTIONS="--experimental-specifier-resolution=node"

Setting NODE_OPTIONS this way, will ensure Node.js uses the experimental specifier for all subsequent executions in the same terminal session

Cucumber Command

Then run the Cucumber command separately:

npx @cucumber/cucumber test/features/*.feature --import test/support/*.js

This is a breakdown of the cucumber command:

  1. npx - package runner tool, which will take the NODE_OPTIONS to determine the flag to run experimental...
  2. @cucumber/cucumber - providing cucumber package name to npx, so it locate the installed version from node_modules dir
  3. test/features/*.feature - path to your feature files with the .feature extn
  4. --import test/support/*.js - path to your step definition files that have the .js extn

Combining all the above, should allow the experimental-specifier-resolution to import the JavaScript files without the .js extension

Remember, if you open a new terminal session, you'll need to set NODE_OPTIONS again.

like image 155
djmonki Avatar answered Sep 20 '25 11:09

djmonki