function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brian KesslerBrian Kessler 

Sfdx can't find npm

I'm currently working on the Trailhead unit Set Up Jest Test Framework (https://trailhead.salesforce.com/content/learn/modules/test-lightning-web-components/set-up-jest-testing-framework?trail_id=build-lightning-web-components)

I am in the directory with my project and I'd like to install Jest into the project.

However, Sfdx seems unable to find npm even though it is clearly installed:
 
D:\tutorials\SFDC2020\Summer20>npm --version
6.14.5

D:\tutorials\SFDC2020\Summer20>sfdx force:lightning:lwc:test:setup
ERROR running force:lightning:lwc:test:setup:  npm command not found. Verify npm is properly installed and try again.

What is wrong and how can I fix it?


 
AbhishekAbhishek (Salesforce Developers) 
Hi Brian,

For all the Trailhead issues please report it here,

https://trailhead.salesforce.com/help?support=home#

https://trailhead.salesforce.com/help

So that our trailhead support engineers will look into it and get back to you.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Regards,
​​​​​​​Salesforce Support.
Brian KesslerBrian Kessler
While I mentioned Trailhead for context, this seems more an Sfdx issue or Lightning issue, but if the support range ever renders, I'll report it there. (An idea that I'm not going to submit at this time: There should be a functionality to move or clone messages here into the support system.)
AbhishekAbhishek (Salesforce Developers) 
Brain If you facing the issue Trailhead then our team will assist you.

And I have checked the Error internally and below are my findings,

Either try to install using sfdx force:lightning:lwc:test:setup or if you are trying to use npm directly, you can install sfdx-lwc-jest (https://www.npmjs.com/package/@salesforce/sfdx-lwc-jest)instead of lwc-jest, see the page you referred https://www.npmjs.com/package/@salesforce/lwc-jest

This package, @salesforce/lwc-jest, has been renamed to @salesforce/sfdx-lwc-jest. Please update your projects to reference @salesforce/sfdx-lwc-jest. @salesforce/lwc-jest will continue to be updated through the Winter '20 release, then development and releases will only happen against @salesforce/sfdx-lwc-jest.

I hope this helps.
Brian KesslerBrian Kessler
Doesn't look like the support bage is ever going to render.  I've been looking at a spinner for the better part of 10 minutes.
No, refreshing the page several times did not help.

I did, however, open up Inspect and I can several instances of ERR_ABORTED 499 and "Failed to load resource: the server responded with a status of 499 () , which seems to be a very non-standard status code.
Brian KesslerBrian Kessler
Currently installing with npm.
Very curious that the install instructions on npmjs.com are trying to tell me to use yarn.  :-p

 
Alain CabonAlain Cabon
It is problem under windows (bug with child_process_1.spawnSync  https://github.com/salesforce/sfdx-plugin-lwc-test/issues/9  )
c:\projects\test-lwc>sfdx force:lightning:lwc:test:setup --json --loglevel=TRACE

With loglevel=TRACE, you see the number of line in the javascript file that fails.

Finally, this command launched by the script that also fails can be launched directly: 
c:\projects\test-lwc> npm install --save-dev @salesforce/sfdx-lwc-jest

The call to npm with child_process_1.spawnSync  fails

setup.js (found with loglevel=TRACE )
installLwcJest() {
        this.ux.log('Installing @salesforce/sfdx-lwc-jest node package...');
        let lwcJestInstallRet;
        const yarnLockExists = fs.existsSync(path.join(this.project.getPath(), 'yarn.lock'));
        if (yarnLockExists) {
            this.ux.log('Detected yarn.lock file, using yarn commands');
            lwcJestInstallRet = child_process_1.spawnSync('yarn', ['add', '--dev', '@salesforce/sfdx-lwc-jest'], { stdio: 'inherit' });
        }
        else {
            lwcJestInstallRet = child_process_1.spawnSync('npm', ['install', '--save-dev', '@salesforce/sfdx-lwc-jest'], { stdio: 'inherit' });
        }
        if (lwcJestInstallRet.error) {
            throw new core_1.SfdxError(messages.getMessage('errorLwcJestInstall', [lwcJestInstallRet.error.message]));
        }
    }

With this technique, you can solve all these problems under windows linked with sfdx (the Trailhead team probably only uses Linux, that is insufficient of course)
Brian KesslerBrian Kessler
Amazing investigation!                                                                                                                     
Alain CabonAlain Cabon
c:\projects\test-lwc>npm install --save-dev @salesforce/sfdx-lwc-jest

+ @salesforce/sfdx-lwc-jest@0.7.1
added 880 packages from 550 contributors and audited 1316 packages in 300.652s


npm is slow and many people prefer yarn.

sfdx force:lightning:lwc:test:setup  encapsulates many checks and that works for "node" but not for "npm". It is not logical and it is a problem on windows especially.

The recommended commands are:
 
npm audit fix
npm install


But that is not enough (still blocked)

setup.js  > you can change it directly ( javascript + node )
const npmVersionRet = child_process_1.spawnSync('node', ['-v']);

		console.log('>>> npmVersionRet error:['+ npmVersionRet.error + ']');
		console.log('>>> npmVersionRet output:['+ npmVersionRet.output + ']');
		console.log('>>> npmVersionRet.status:['+npmVersionRet.status + ']');

		if (npmVersionRet.error !== undefined || npmVersionRet.status !== 0) {	
            throw new core_1.SfdxError(messages.getMessage('errorNpmNotFound'));
        }
 
c:\nsp\projects\test-lwc>sfdx force:lightning:lwc:test:setup --json --loglevel=TRACE

>>> npmVersionRet error:[undefined]     (my own trace with console.log)
>>> npmVersionRet output:[,v11.9.0,]
>>> npmVersionRet.status:[0]

{
  "status": 1,
  "name": "SfdxError",
  "message": "Error installing @salesforce/sfdx-lwc-jest: 'spawnSync npm ENOENT'",
  "exitCode": 1,
  "commandName": "Setup",
  "stack": "SfdxError: Error installing @salesforce/sfdx-lwc-jest: 'spawnSync npm ENOENT'\n    at Setup.installLwcJest (C:\\Users\\<my user>\\AppData\\Lo
cal\\sfdx\\client\\7.61.0-af69066d0d\\node_modules\\@salesforce\\sfdx-plugin-lwc-test\\lib\\commands\\force\\lightning\\lwc\\test\\setup.js:134:19)\n
   at Setup.run (C:\\Users\\<user>\\AppData\\Local\\sfdx\\client\\7.61.0-af69066d0d\\node_modules\\@salesforce\\sfdx-plugin-lwc-test\\lib\\commands\\f
orce\\lightning\\lwc\\test\\setup.js:59:14)\n    
at Setup._run (C:\\Users\\<user>\\AppData\\Local\\sfdx\\client\\7.61.0-af69066d0d\\node_modules\\@sal
esforce\\sfdx-plugin-lwc-test\\node_modules\\@salesforce\\command\\lib\\sfdxCommand.js:91:51)",
  "warnings": []
}
 
c:\projects\test-lwc>npm install npm@latest -g

+ npm@6.14.5
updated 1 package in 54.115s

c:\projects\test-lwc>npm install node@latest -g

+ node-win-x64@14.4.0
added 1 package and audited 1 package in 4.453s
found 0 vulnerabilities

+ node@14.4.0
removed 1 package and updated 1 package in 9.47s

c:\projects\test-lwc>npm install sfdx@latest -g

+ sfdx@1.4.2
updated 1 package in 18.906s
On windows, you have probably two installations of node and you can uninstall the "exe" installation in conflct with the npm version above.

node -v  often gives an old windows "exe" installation instead of the latest version above (14.4)..

"message": "Error installing @salesforce/sfdx-lwc-jest: 'spawnSync npm ENOENT'",

First solution at the top of this post:

npm install --save-dev @salesforce/sfdx-lwc-jest
 
Alain CabonAlain Cabon
On windows, we still need the exe version     https://nodejs.org/en/download/
That is not the same latest version via npm (v12 instead of v14)

C:\>node -v
v12.18.1

C:\>npm -v
6.14.5

C:\>npm install sfdx-cli --global

+ sfdx-cli@7.63.0
added 47 packages from 40 contributors, removed 39 packages, updated 67 packages and moved 6 packages in 345.76s

First solution is still:

npm install --save-dev @salesforce/sfdx-lwc-jest
 
Mike ArthurMike Arthur
Same error here on Windows.
I ran
 
npm i @salesforce/sfdx-lwc-jest

from terminal in VS Code and it installed successfully as verified by
 
node node_modules/@salesforce/sfdx-lwc-jest/bin/sfdx-lwc-jest

The package page is here:
https://www.npmjs.com/package/@salesforce/sfdx-lwc-jest (https://www.npmjs.com/package/@salesforce/sfdx-lwc-jest" target="_parent)