Sublime Text 3: Build System - node.js. NPM module not executing - javascript

I'm trying to execute node-dev in a sublime text 3 build system. node-dev is in my path:
Yet when I run this build script:
{
"cmd": ["node-dev", "$file"],
"selector": "*.js"
}
I get this error, which also shows that npm is in my path.
yet when I run with the same build script using node instead of node-dev it executes just fine.
I've also tried to include the "path" variable pointing at the node-dev bin folder, which didn't help at all.
Any help would be appreciated. Thanks.

Following worked for me in Sublime Text 3 on Windows
Tools -> Build System -> New Build System...
Enter the below text in the new file
Save the file as "nodejs.sublime-build"
{
"shell_cmd": "node ${file}",
"selector" : "source.js"
}
Prerequisite is to have node.js installed

Sublime text docs:
https://www.sublimetext.com/docs/build_systems.html
shell
Optional. If true, cmd will be run through the shell (cmd.exe, bash…)
Try to add "shell : true
{
"cmd": ["node-dev", "$file"],
"selector": "source.js",
"windows" : {
"shell": true
}
}

The command is incorrect for Sublime Text 3 :)
This is one example of running node as build system:
{
"shell_cmd": "taskkill /F /IM node.exe & node ${file}"
}
Please note that the array-version doesn't work like in Sublime Text 2.

For macOS, this worked for me on Sublime Text 3:
{
"cmd": ["node","$file","$file_base_name"],
"working_dir": "${project_path:${folder}}",
"selector":"source.js"
}
Selector Note
My selector setting was:
"selector":"*.js"
and OdatNurd advised that:
The reason is that the selector is not correct; it doesn't match file
names, it matches syntax scopes (i.e. it's based on the syntax in use
regardless of file extension); changing it to source.js from *.js
should get it working.

If you are a windows user.
Try applying the following snippet
{
"selector": "source.js",
"cmd": ["C:\\Program Files\\nodejs\\node", "<", "$file"],
"windows": {
"shell": true
}
}
Save this as node.sublime-build file.
For more info you can refer to http://docs.sublimetext.info/en/latest/ for more.

Related

NPM Scripts: Pass one script command json output to another script command as argument value

I am trying to inject a json as an argument value within npm script, like this:
(https://medium.com/typeforms-engineering-blog/codeceptjs-puppeteer-setting-up-multiple-configurations-32d95e65adf2)
"scripts": {
"example": "bash -c 'cat ./sample.json | tr -d ' \t\n\r''",
"test:override": "codeceptjs run --steps --config ./codecept.conf.js --override <expecting_output_json_here_from_example_script>",
}
The first script command 'example' executes well and displays the output json correctly in the console (same as input):
{
"helpers":{
"Puppeteer":{
"url":"http://yahoo.com",
"show":false,
"windowSize":"1920x1080"
}
}
however, I couldn't find the way to pass it as an argument value for the --override in the "test:override" script
I have tried various StackOverflow questions/answers, however, I couldn't find a way to achieve it.
I am using Git Bash in VS Code in Win10.
Please suggest a solution/alternate approach or point me to the right document/SO question.
To the best of my knowledge and efforts, I believe that it is not a duplicate question.

Could not correctly pass argument to nodejs using pm2

I used to start a nodejs script with this command
node cli/myscript.js -c configs/some-config.json
I tried to start the same script using pm2. I found here a post handling the same theme.
I tried this :
pm2 start cli/myscript.js --node-args="-c configs/some-config.json"
I get a syntax error on the config file which I don't get when I don't use pm2.
SyntaxError: Unexpected token :
0|myscript | at checkScriptSyntax
I also tried this and get the same error:
pm2 start cli/myscript.js -- -c configs/some-config.json
It seems like that pm2 tries to execute the config as a js file.. because the config file is a valid json.
You can pass the file name in the same-config.json.
{
"apps" : [{
"name" : "myscript",
"script" : "cli/myscript.js",
"watch" : true,
"env": {
"NODE_ENV": "development"
}
}]
}
Then you can run the node server by following command -
pm2 start same-config.json
For more details please refer PM2 docs

How to properly re-configure JSPrettier after an update with a breaking change?

I am trying to make Jsprettier work in Sublime, but the formats I set up can't be applied, because I keep getting an error when I want to save a document (jsprettier: format failed). When I open the console to see what the error is it says: The system cannot find the path specified. (CR).
The settings in the user settings tab of jsprettier are the following:
{
"prettier_cli_path": "/c/Users/Adri/AppData/Roaming/npm/prettier",
"node_path": "/c/Program Files/nodejs/node",
"auto_format_on_save": true,
"prettier_options": {
"printWidth": 120,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"parser": "flow",
"semi": true,
"tabs": false
}
}
Thank you
In your terminal run the following:
which node
which prettier
Copy the result of the output for each step and in SublimeText3 proceed to:
Preferences --> PackageSettings --> JsPrettier --> Settings-User
In the user settings build your file as such:
{
"prettier_cli_path": "output from terminal here",
"node_path": "output from terminal here",
"auto_format_on_save": true,
}
I found that when JsPrettier updates it erased these settings on me and needed to re-enter them...
Hope that helps.
tested on: Sublime Text 3.2.2, MacBook Pro
Install Js​Prettier globally using npm:
npm install --global prettier
Install JsPrettier from Package Control by:
Sublime Text-->Preferences-->Package Control-->type and enter: Install Package-->type and enter: JsPrettier
restart Sublime Text
In case this helps anyone:
While this installed easily on one computer, I struggled with it on a second one.
After being required to set the node and prettier_cli path, I got node errors.
What worked in the end was setting the prettier_cli_path to prettier.js within the prettier node_modules directory as such:
C:/Users/Me/AppData/Roaming/npm/node_modules/prettier/bin-prettier.js
Problem solved!
I just took the first 2 lines out (paths) and now it works!
In my case, I had to fully restart Sublime 3 after it has installed a new Sublime version.
If someone is having trouble with this even now, it may help to know that Prettier requires node >= v10.
To get this to work, you may have to set your node version as >= 10, I personally prefer 12 or 13.
You can do this either by using nvm alias default 12 fornvm or n 12 for n
Now, update the user settings file to make sure prettier has the right paths to prettier and node. Find the paths to node and prettier using which
which prettier
which node
Add the paths and flags to auto format on save as desired
{
"prettier_cli_path": "/Users/user/.nvm/versions/node/v12.13.1/bin/prettier",
"node_path": "/Users/user/.nvm/versions/node/v12.13.1/bin/node",
"auto_format_on_save": true,
"format_on_save_extensions": ["tsx", "json"]
}
Now reload sublime
You're running an outdated version of Prettier. Please update again to the latest version, which is v1.6.1 (as of Sept. 13, 2017).
If you want to use v1.5.x on purpose, you can add the --no-config option to the additional_cli_args setting,
and tell Prettier not to attempt to find config files.
json
{
"additional_cli_args": {
"--no-config": ""
}
}

How to join command of npm?

package.json:
"scripts": {
"cpFile": cp ../template/index.js /src/view/home/
}
and I run:
npm run cpFile fileName.js
I want it to execute
cp ../template/index.js /src/view/home/fileName.js
but it doesn't work.
First of all you need quotes around your command
"scripts": {
"cpFile": "cp ../template/index.js /src/view/home/"
}
then if you need to pass in args, you will need to use the args delimiter
> npm run cpFile -- fileName.js
>> cp ../template/index.js /src/view/home/ "fileName.js"
probably not what you want
you can read about it here too:
> npm help run
Edit
you will probably need to pass in the whole path to get what you want.
"scripts": {
"cpFile": "cp ../template/index.js "
}
Then:
> npm run cpFile -- /src/view/home/fileName.js
The serialization of JSON doesn't work with functions, RegEx nor error-objects. In addition this is not a valide JSON format you have posted.
JSON starts and ends with a curly brace { } and you will need quotes around most names and values you created, except numeric values and booleans.
{ "scripts": { "cpFile": "cp ../template/index.js /src/view/home/" } }
There is also no method to run JavaScript without having plugins installed, like most browsers do.
Maybe curl can help to execute the script.

JavaScript Build system using Node.js error in Sublime Text 2

I'm using Node.js to make a build system in Sublime Text 2 to build JavaScript code. I know how to setup basic build systems in Sublime, but this one is really giving me problems.
JavaScript example code:
var end = 10;
for (var i = 0; i < end; i++) {
console.log("hello world!");
};
1st try:
{
"cmd": ["node", "$file"]
}
returned this in the console: [Finished in 0.1s]
2nd try:
{
"cmd": ["node", "$file", "$file_base_name"],
"working_dir": "${project_path:${folder}}",
"selector": "#.js"
}
returns the same thing.
Any help would be much appreciated (I've spend over 2 hours searching google).
EDIT: Fixed For to for : Same result.
Here's a Javascript build system I use on my Mac with Sublime Text. It uses the Webkit engine (SquirrelFish) which comes with OS X, and therefore doesn't require anything special be installed (not even Node.js):
{
"cmd": ["/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc", "$file"],
"selector": "source.js"
}
Tips:
Use "print(foo)" from within your script to return results to the Sublime console
Save your .js file to disk at least once
If ".js" files are not automatically selected, ensure you have no other build systems for ".js" in "~/Library/Application Support/Sublime Text 2/Packages/". Alternately you can manually select the appropriate system from "Tools > Build System"
Hope this helps.
In Sublime Text 2 you must save your .js file before you build, or the result above will happen.
Simple fix is to save the file (obviously)!

Categories