How to use jsdoc in my script running on node.js? - javascript

I would like to use jsdoc module to extract documentation entries from some source code.
I have installed jsdoc module and I can use jsdoc in the command line.
But when I require("jsdoc") in my code, nodejs throws an error saying Cannot find module 'jsdoc'.
I have nodejs v0.8.25 and JSDoc 3.3.0-alpha2. It is installed both locally and globally.
I can use jsdoc command and I have jsdoc in my node_modules folder.
I cannot see where the problem is.
Where can I find some documentation about jsdoc other than how to use it in the command line interface or how to document js source code. I would like some API documentation.

I was looking to use jsdoc as a library too, and I stumbled on the same issue as you did.
You have done nothing wrong, it was just never intended to be used as a library.
There is an issue on the jsdoc's github project page relating to this. Hegemonic, the main contributor of JSDoc said:
JSDoc isn't really designed to be used as a library
You might want to express your desire to for creating an API for nodejs on the issue linked above.
The require('jsdoc') fails because there is no main field declared in the jsdoc's package.json. It might default to 'index.js' or 'main.js', but there is no such file either.

It sounds like you haven't installed jsdoc correctly. Run npm install -g jsdoc from your command line. If you run this inside a new terminal window that's in the default directory you shouldn't have any problems. I've had issues where I've tried to install a node module globally from my current working directory and for whatever reason the module doesn't install globally.

If it is about an automatic documentation tool in general, you could also use YUIDoc.
Following these three steps, you will have your automatic documentation:
Installing it: npm -g install yuidocjs
Format your comments regarding the YUIDoc Syntax Reference
In your console run yuidoc . on top of your JS source tree

Related

How to configure create-react-kotlin-app in order to get kotlinx.serialization working

I've been trying to get the kotlinx.serialization library working with Kotlin/JS using the create-react-kotlin-app
However, I am not so familiar with the huge nodeJS and webpack mess.
I did the following:
1) Downloaded the kotlinx.serialization library from maven repository a located it aside the nodeJS dependencies, because there is so far no any npm module for this.
2) Modified the node_modules/#jetbrains/kotlin-webpack-plugin/plugin.js in order to compile my library, in particular I added this row to the prepareLibraries function:
opts.libraries.push(opts.packagesContents[0]["_where"]+"/lib/kotlinx-serialization-runtime-js/build/classes/main/kotlinx-serialization-runtime-js.js")
3) Modified the last line in node_modules/kotlin-compiler/bin/kotlinc in order to enable the the serialization compiler plugin:
${JAVACMD:=java}" $JAVA_OPTS "${java_args[#]}" -cp "${kotlin_app[#]}" "${kotlin_args[#]}" -Xplugin=$KOTLIN_HOME/lib/kotlinx-serialization-compiler-plugin.jar
The thing is that when I run the dev server using npm start, it compiles and runs as expected, but when I run npm run build, I got the following error:
Creating an optimized production build...
Failed to compile.
warning: flag is not supported by this version of the compiler: -
Xplugin=/home/Project/archetype-frontend-kotlin/node_modules/kotlin-compiler/lib/kotlinx-serialization-compiler-plugin.jar
npm ERR! code ELIFECYCLE
And of course when I remove the -Xplugin argument, the application is throwing exceptions like this:
Can't locate argument-less serializer for class Pu…h as lists, please provide serializer explicitly.
(There is an issue on GH related to this https://github.com/Kotlin/kotlinx.serialization/issues/278)
Thanks for any help
Okay, I think I managed to make it work.
Here are the steps as I remember them:
as suggested in https://youtrack.jetbrains.com/issue/CRKA-84, did npm eject and added plugin: require.resolve('kotlin-compiler/lib/kotlinx-serialization-compiler-plugin.jar'), to webpack-kotlin-*.js files (after new KotlinWebpackPlugin).
downloaded the compiled runtime from maven (https://dl.bintray.com/kotlin/kotlinx/org/jetbrains/kotlinx/kotlinx-serialization-runtime-js/0.9.0/) and unpacked it to node-modules/kotlinx-serialization-runtime-js
hacked kotlin-compiler.js by adding the library path (there must be a better way!):
options.libraries.join(isWindows ? ';' : ':') + ":<absolute_path_to_project>/node_modules/kotlinx-serialization-runtime-js/kotlinx-serialization-runtime-js.meta.js"
copied kotlinx-serialization-runtime-js.js and kotlinx-serialization-runtime-js.js.map to node-modules/.cache/kotlin-webpack.
After this yarn start appears to be able to compile some test code that uses kotlinx-serialization, however, Idea fails to find where kotlinx.serialization module comes from.
I hope this answer inspires someone who is willing to reverse-engieer this build system to make a better fix.

Cannot use typescript class from other local typescript module using browserify

I have a common npm module containing TypeScript which is an node module providing some classes and functionality. This module is packaged via browserify + tsify and also exports the declaration files and built with gulp.
I have another npm module using TypeScript which imports this module locally via file:../modulename and tries to use functionality from the first module. The second module is also packaged via browserify + tsify and built with gulp. The compilation of the second module works via npx tsc, but fails if I execute the gulp build file with the error message
Error: Cannot find module './AbstractClass' from 'folderInOtherModuleContainingTheJSFiles'
Interestingly it sometimes fails with this Class sometimes with another, so it seems there is also some kind of concurrent processing.
I extracted the failing code from my project and created a minimal example with this behavior here.
npm version: 5.6.0
node version: v9.5.0
For anybody who might come to this post and face the same error.
I opened an issue at tsify too, thinking it has something to do with the plugin, because npx tsc worked.
Luckily the issue has been resolved, although with a workaround. There seems to be a name collision which causes browserify to think that a require call, which is a variable, in the resulting bundle needs to be resolved, but can't. The workaround is uglifying the resulting bundle, so that the conflict does not happen. More details in the above linked issue.

Visual Studio Code not performing error checking in Javascript

I've tried following these instructions:
https://code.visualstudio.com/Docs/runtimes/nodejs
I am not getting the green/red swiggly lines at all. Is there something I'm missing?
You can also see the same thing in this video:
https://youtu.be/sE8_bTEBlFg?t=1m37s
As far as I know, they're running the default editor. I've tried installing typings and typescript using npm. I've Followed that tutorial to get Javascript intellisense for node.js, but I fail to get either error/warning checking or any type information for node.js modules.
Is there a location the type files should be installed to in order to make them global to every JS project you create in VS Code?
OK, so I managed get get some code suggestions working after reading up online. Without using the whole Typings tools, I acquired node.d.ts (found it on my computer inside C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions) and placed that in my project's directory structure inside the ".vscode" folder. At the top of my .js file I added the following line:
/// <reference path=".vscode/node.d.ts" />
The code seems to be recognized now.
I read up on this tip here: How to Import Intellisense files into vsCode (Visual Studio Code)
If you are using ESLint and you have an .eslint.js file in the project root you also need the eslint dependency installed in the project. Otherwise, if there is a .eslint.js file but the ESLint dependency is not installed Visual Studio Code will report nothing in the editor.
Maybe you didn't use the -g flag to install them globally? Alternately, perhaps it's a missing jsconfig.json file?

How can I use TypeScript compiler through nodejs?

I have a sample code and saved it to a file such as hello.ts
After installing nodejs on windows use below command for installing typescript
npm install -g typescript
How can I compile hello.ts with node.js directly?
When I install "TypeScript 1.6 in VS2015" and use tsc.exe don't have any problem but I want to use node.js instead of VS 2015 extension
Please guide me generate .js and .ds through Node.js
Run tsc in the command line, you'll have the help page. Compiling a script is easy, just tsc hello.ts in the folder containing your script, you'll get a hello.js file.
Please guide me generate .js and .ds through Node.js
You have two options:
Run tsc.js as a node script
Use typescript as an npm module
Run node tsc.js
This is the approach taken by some tools e.g grunt-ts. You basically just call spawn on the current process process.execPath passing in the other commands as args (-d).
One sample
Run TypeScript as a node module
If you are playing with the typescript compiler API highly recommend NTypeScript See the Readme for reasons.
The TypeScript compiler provides a simple function called transpile that you can use to get the expected output and then write it out to disk yourself.
PS: I have some docs on the TypeScript compiler internals here : https://basarat.gitbooks.io/typescript/content/docs/compiler/overview.html
My preferred way to get going is to use typescript-require.
this library basically adds nodejs support for typescript.
your index file should be a javascript file and it will look like this
require('typescript-require');
require('./src/main.ts');
and main.ts is a typescript file.
Here's a programatically way from TS/Node to run the equivalent of tsc from within a code itself (Note: this uses the TypeScript Compiler API):
https://gist.github.com/rnag/0d8fe2e72dc7b48743c13f9ca8837a4c

VS2015 Convert javascript to typescript and node modules

I developed a javascript web app with npm and webpack. Now I converted all those .js files to .ts by using the powershell command stated here. The succeeding actions in the link is using grunt; I want to directly use VS2015 Typescript project but I cannot find any reference on the net about what to do with the node_modules and how I can fully convert all my package.json and webpack into Typescript project. The Task Runner Explorer in VS2015 only supports Grunt and Gulp tasks.
I recommend going with the "bare-foot" solution first. I'd rely much less on VS2015. It's maybe the best IDE available, but JS and TS projects can be handled from command line without relying on the magic of the IDE. This way you can gain a deeper understanding of these technologies and I think now it would be easier too.
I recommend the following steps:
create a tsconfig.json in the root folder. Read around, there's plenty of info available. Just one hint: use 'filesGlob' to specify the files to compile.
use TSD to get the .d.ts files of the libs you use from DefinitelyTyped. You might create, or at least just declare, the missing packages.
run 'tsc --project .' from command line to compile everything. You'll see the errors that are to be solved.
I'm typing from mobile, I can edit the codes tomorrow. Have any comments?

Categories