I'd like to use the node --debug-brk feature described in a previous answer and the jest docs to debug in npm/yarn 1 based projects, but node_modules/ is not present in Yarn 2 / PnP (Plug n Play) based projects, so those instructions won't work for me.
The usual way to access a binary in yarn 2 of yarn run --inspect-brk jest --runInBand launches a debugging session from the terminal, but when connecting to Chrome's Remote Target inspector via chrome://inspect, we get an error message about being unable to connect to the jest.js binary, since it's a path to a .zip file.
Uncaught Error: Cannot find module '/Users/MY_USERNAME/PATH_TO_MY_PROJECT/.yarn/cache/jest-npm-24.9.0-8ddb425e99-2.zip/node_modules/jest/bin/jest.js
What workarounds are available without downgrading to Yarn 1?
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest",
"skipFiles": ["<node_internals>/**"],
"cwd": "${workspaceFolder}",
"runtimeExecutable": "yarn",
"runtimeArgs": ["run", "--inspect-brk", "jest"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
Update: the workaround reported below is necessary for Node 12.15 and below. After 12.16.1, you can use the following command directly:
yarn run --inspect-brk jest --runInBand
The fix which was added to node 12.16.1 is here.
Archived answer for older versions of Node, 12.15 and below
One option is to use yarn unplug before using yarn run
yarn unplug jest
After that, the following works with Jest in Yarn 2 and Chrome's chrome://inspect:
yarn run --inspect-brk jest --runInBand # any additional jest args
A downside of this approach is that you may need to undo the unplug before checking in your branch, since it's described as a short term measure rather than a long term state to leave a dependency in.
You can also use the yarn bin command to accomplish this. I was able to run our jest tests using node and some debugging flag helpers (in a Yarn pnp context) using this command:
yarn node --inspect-brk --expose-gc $(yarn bin jest) test --runInBand --silent --logHeapUsage
Credit - Found the solution posted in this jest issues thread.
Related
Just getting started with yarn v3 but hit a bit of a problem with jest not running they just hang indefinitely with no output at all.
Recreation steps
yarn set version berry
yarn init
yarn add -D jest
yarn jest
// package.json
{
"name": "yarn-again",
"packageManager": "yarn#3.2.0",
"devDependencies": {
"jest": "^28.1.0"
}
}
Anyone seen this before?
Bump node to 16.14.2 or upper or try to run yarn jest --runInBand and check errors
This is the first time I'm using Volta, so bear with me.
I have installed globally typescript, node and yarn
yarn -v >> 1.22.10
node -v >> v14.15.4
npm -v >> 6.14.10
These commands work inside and outside my project folder, with the same results.
However if I use yarn build from inside vscode the output is an error stating:
System cannot find the path specified
If I do the same from outside vscode I get the same result:
If I go to the node_modules/.bin folder inside vscode, the command still doesn't work (this time I just only run tsc). The error is the following:
The term tsc is not a cmdlet recognized command, function, script file or executable program. Verify if the name is written correctly or, if there is an access route, verify the route is correct and try again.
But if the command is executed from outside vscode in a cmd window, it works as expected, because tsc is really there:
Additionally, if I run npm run build inside vscode, the command works as expected. This is my package.json:
{
"name": "socket-server",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"scripts": {
"build": "tsc",
"dev": "yarn build --watch --preserveWatchOutput",
"lint": "eslint src --ext js,ts",
"test": "jest"
},
"devDependencies": {
"eslint": "^7.19.0",
"jest": "^26.6.3",
"typescript": "^4.1.3"
},
"volta": {
"node": "14.15.4",
"yarn": "1.22.10"
}
}
I do suspect of volta because volta is managing yarn version, but no npm; but I don't really know what's the issue.
I'm using Windows and my PATH has the following entries:
What am I doing wrong?
Edit: Yes, Shell command shortcut exists:
the problem is about vsCode, you should run code . in cmd because if you Open the Command Palette (Ctrl + Shift + P) and type
Shell Command: Install 'code' command in PATH
you won't see noting, after running code . in cmd when you should see like this photo, every things will be fine
I’m not sure for Windows, but usually the scripts in node_modules/.bin are symbolic links to scripts. For instance, node_modules/.bin/tsc might point to node_modules/typescript/bin/tsc.
It works outside of the directory because then it uses the global version of tsc.
Seeing your error, I’m suspecting that the symlinks are broken. Maybe just try to remove node_modules directory and redo an npm install.
PS: I’m not familiar with Volta, but it looks more like an NPM problem.
I'm on a windows 10 machine trying to run a build script from the git bash terminal.
On my terminal node is recognized just fine, for example I get the version when I run node --version.
But running the build script fails with the following error:
'NODE_OPTIONS' is not recognized as an internal or external command,
operable program or batch file.
I'm guessing I need to add something to my PATH variables to get this to work, but what?
Use cross-env package which easily sets environment variables.
Step 1:
Install cross-env from npm
npm i cross-env
In your package.json file (In this example your need is to run 'start' command which has 'NODE_OPTIONS')
{
"name": "your-app",
"version": "0.0.0",
"scripts": {
...
"start": "NODE_OPTIONS=<your options> <commands>",
}
}
Step 2
Add 'cross-env' in the script which you need to run NODE_OPTIONS. (In this case 'start' script)
{
"name": "your-app",
"version": "0.0.0",
"scripts": {
...
"start": "cross-env NODE_OPTIONS=<your options> <commands>",
}
}
For me installing the below mentioned package solved the problem
npm install -g win-node-env
Not a PATH issue, NODE_OPTIONS is an ENVIRONMENT VARIABLE that needs to be set before starting your build. To set en environment variable in Windows 10 you need to use the set command in a terminal mode. See this article on SUPERUSER forum to learn more.
In your case, just add set before NODE_OPTIONS and that will fix your issue.
Here's how to integrate it in package.json:
...
"scripts": {
...
"build": "set NODE_OPTIONS=--max_old_space_size=4096 && next build"
...
}
...
A way to launch both the node process and the debugger via F5, which does not require wrestling with env vars.
Make sure .vscode/launch.json is deleted.
1. Open the Run & Debug pane
2. Click on Node.js
3. DO NOT click on "Run script: dev" directly, instead click on the cog next to it
4. Your launch.json should look similar to:
{
"configurations": [
{
"type": "node-terminal",
"name": "Run Script: dev",
"request": "launch",
"command": "yarn run dev",
"cwd": "${workspaceFolder}"
}
]
}
I use a task in no gulp to run my project in developer mode for a long time, however today, when trying to run the project, I got this error:
I use the node with NVM, so I tried to reinstall NVM and the Node version v8.9.3, but that did not solve the problem. I tried reinstalling NPM and it also did not fix the problem.
The error happen when press F5 to run command:
Configuration in VSCode:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Gulp Run Dev",
"program": "$ {workspaceFolder}/node_modules/gulp/bin/gulp.js",
"args": [
"rundev"
]
}
]
}
I noticed that when I run the task manually in Terminal it works:
gulp rundev
I did not perform any updates or changes to the project modules and did not perform any updates to the VSCode.
I noticed that there was an inconsistency in git (I can not say how it happened) but while pointing to the branch 'develop' the code displayed, it was the first commits of the project. Then delete the project folder and re-clone it from the repository. I ran npm install and after installing the dependencies, I set up the launch.json file again and everything is back to normal.
I want to use mocha to test my TypeScript/Angular2 project. I tried to use ts-node as described here:
npm install -g ts-node
but when running
mocha --require ts-node/register -t 10000 ./**/*.unit.ts
I get an error
Cannot find module 'ts-node/register'
What am I missing here?
Since the answer that works for a lot of people appears to be hidden in the comments, I'll post it as an actual answer to the question, now that it appears the question has been reopened.
I had this problem as well. Not sure why this Q has been closed. but installing ts-node locally fixes this. npm install ts-node --save-dev
Thanks #Anita, as this was the answer that worked for me too.
Wow, a silly mistake can cost you time. I was facing the same issue when I was trying to debug my nodejs application. The mistake I had done was that I have created my .vscode folder outside of my nodejs app folder(the directory which had node_modules in it). When I moved my .vscode to that folder, everything work fine. Below is my launch.json file.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "index",
"args": [
"src/index.ts"
],
"runtimeArgs": [
"-r",
"ts-node/register"
],
"cwd": "${workspaceFolder}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
}
],
"compounds": []
}
I know this is kind of old, but I ran into this too and wanted to offer the solution I'm currently using.
I installed ts-node globally using sudo npm i -g ts-node. To make this work with mocha, I just had to give mocha the absolute path to the module, like this:
mocha -r /usr/lib/node_modules/ts-node/register test/*Test.ts
Hope that helps someone else.
Try this command instead:
mocha --compilers ts:ts-node/register,tsx:ts-node/register
which works for me.
I have mocha and ts-node installed as dev dependencies in my package. I'm also using pnpm. I originally had a script for test that was defined as:
pnpx mocha -r ts-node/register '*.test.ts'
When this stopped working with the same error as reported in this question I fixed it by making the -r path explicit:
pnpx mocha -r ./node_modules/ts-node/register '*.test.ts'
I'm still confused as to why the original version stopped working.
Use /node_modules/ts-node/register in place of ts-node/register. So in your case it'll become:
mocha --require /node_modules/ts-node/register -t 10000 ./**/*.unit.ts
and make sure ts-node is installed locally in your project like:
npm install ts-node #types/node typescript
This solution doesn't need you to install ts-node globally. And it works cross platform and for everyone.
Assuming you rather not install ts-node locally and would prefer to use the globally installed node_module. The following examples show what to do for Windows and assumes you're using NVM. If not using NVM, replace NVM_SYMLINK with C:\Program Files\nodejs
Example for command line:
node -r "%NVM_SYMLINK%\node_modules\ts-node\register" script.ts arg1 arg2
Example for bash:
node -r "$NVM_SYMLINK/node_modules/ts-node/register" script.ts arg1 arg2
Example for vscode launch.config
"configurations": [
{
"name": "utils/roll_browser",
"type": "node",
"request": "launch",
"runtimeArgs": [
"-r",
/* Slower startup. Runs full typescript validation */
// "${env:NVM_SYMLINK}/node_modules/ts-node/register"
/* Faster startup. Doesn't check types or verify the code is valid */
"${env:NVM_SYMLINK}/node_modules/ts-node/register/transpile-only"
],
"args": ["${workspaceFolder}/utils/roll_browser.ts", "chromium", "756141"],
},
]
I have the same issue while using "jasmine-unit-test-generator". I fix it by runing npm link ts-node.
you can refer to https://github.com/TypeStrong/ts-node/issues/565
I know that I'm late but I had the same problem today
You have to give to the --require flag an absolute path. I had the same error and I solved It by going from this:
"script":{
.....
"test":"env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha --require /node_modules/ts-node/register 'test/**/*.ts'"
}
To this with a relative path for the flag:
"script":{
.....
"test":"env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha --require ./node_modules/ts-node/register 'test/**/*.ts'"
}
The key thing is the dot in the ./node_modules.
Of course you have to have your module installed locally or globally installed and linked to the node project
If you don't want to use the local version of ts-node (the one being used by your project), and instead want to use the global version of ts-node you installed, you can do this...
which ts-node # to find the path for the global ts-node
{
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"request": "launch",
"name": "ts-node",
"type": "node",
"program": "THE_PATH_YOU_GOT_FROM_THE_WHICH_CMD",
"args": [
"${relativeFile}"
],
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart"
}
]
}
You don't need to use the node -r runtime argument if you're using your global installation of ts-node.
Error:
module.js:328
throw err;
Error: Cannot find module 'ts-node'
Solution: Following command solves the issue.
<b>npm install ts-node --save-dev</b>
(Installs ts-node as a development dependency for your project)