I am newbie in Node.js.
Whenever I want to install a package, I do it like
"npm install -g package-name"
What I have seen inside my node applications there is a dir "node_modules" been created and all the installed modules are there.
Then I want to use "grunt" for automating my tests for frontend javascirpt unit tests.
And I ran the command for installing "npm install -g grunt" but when I go inside my test directory and run grunt I get "Fatal error: Unable to find local grunt." But if I install it inside the "test" directory it works fine.
My project structure like below:
-backend
-tests
-model
-node_modules
-package.json
-others
-frontend
-tests
-js
-package.json
-node_modules
-others
How I can manage node packages from a single pacakge.json and run the tests separately in frontend and backend? What are the optimized way of doing this stuff?
Thanks in advance.
The -g flag in npm will install the module globally -- this is how things like grunt work as a CLI tool, but for your dependencies, you probably just want them to be installed in your node_modules folder, defined in your package.json.
Are you browserifying your front-end modules? You most likely do not want both your front and back end to have the same set of dependencies, best to keep them separate package.json manifests. Are your frontend modules just grunt tasks? Running grunt in either frontend or backend directories will call the gruntfile only there.
Keep these two directories separate -- will save a lot of headache in the future.
Related
I'm not familiar with npm so I might be holding the wrong end of the shovel here...
There is a package on npm that I would like to modify and use in my own project. The package is angular-crumbs. I forked the source repo (https://github.com/emilol/angular-crumbs) into my own account (https://github.com/capesean/angular-crumbs) and then run npm install capesean/angular-crumbs -force. However, this produces a node_modules folder in my project that hasn't been built (and whatever else - as I understand it) with the commands in the source repo's package.json file:
"build": "npm run clean && npm run transpile && npm run package && npm run minify && npm run copy"
i.e. it doesn't have the types, the correct package.json file, etc.
So my question is, how do I get the properly-built files (including type definitions, etc.) from my own repo to install or build-after-installing in my target project?
I am not sure about what you're trying to do, are trying to work on the
angular-crumbs source code, or are you trying to use it in your own project as a dependencuy ?
Anyway, running npm install will install all your dependencies so that you can directly use them in your project, those dependencies don't need to be built after they are installed.
In your case you seem to have an angular application (which is completely different from node.js), usually to start an angular app you can run ng serve which will build your source code and run an angular server so you can access it on localhost.
Created new folder and did npm install serve in it.
It created package-lock.json and node_modules/ folder.
When I run in the same folder serve it shows error:
command not found: serve
What is the way to install?
I am using: npm#6.5.0
My dev environment is MACOS
I read a great many pages on this topic and nothing worked until I tried the following
./node_modules/.bin/serve -s build
Also if you are using VS CODE you may want to bring up the terminal window outside of VS CODE - this seems to have snared a lot of people.
First of all, you should start your project running
npm init
This will create the package.json file.
Then, you can install the serve package globally.
npm install -g serve
And now you can run serve.
The serve binary was not found because the operating system cannot locate it in the PATH environment variable.
When you do the npm install serve command. The serve module is only installed into the node_modules directory found under the the project folder. Unless you explicitly include the absolute path of this node_module directory as part of your PATH env var, the OS won't know where to find serve.
Like others say, the typical practise would be to install the module using the -g flag. G means global.
When -g is used, npm will put the binary in its node directory somewhere and this this directory would have been included as part of your PATH when you install node, thus making the any new binary discoverable.
If the node.js module has a "command" and you want to run it without installing the module globally(npm install -g serve). You can run it like ./node-modules/.bin/command from the root folder of the project.
Now, what is generally used is npx, so that you can from within a project easily run any of the binaries within its local node_modules or your system's global node_modules/ and any other commands on the $PATH.
For example, here we install webpack as a local dependency. You can image doing this in a folder after running npm init. Then we run webpack without having to worry about referencing the bin file:
$ npm i -D webpack
$ npx webpack
i am trying to build a monorepo , which consist of a react and node js application so the frontEnd folder which is created using react app and backEnd folder which consist of all server code.
I have done the below steps
installed lerna globally
created a new folder and initialized the repository.
inside the new folder run lerna init, which created 2 files lerna.json, package.json and 1 folder as packages.
inside packages folder i run create-react-app frontEnd, it created a new react application
run the command mkdir backEnd, to created the backEnd folder in the packages folder
now my packages folder consist of two folders frontEnd and backEnd.
the frontEnd consist of a package.json which comes with create-react-app
questions
Do i need to remove the package.json from the frontEnd folder
how i configure the lerna.json and package.json in the root folder.
how can i run the application?
i have searched but i am not getting the solution to make a react-nodejs application with lerna using create-react-app
I did this task here
Do i need to remove the package.json from the frontEnd folder
No, each project in the /packages folder may have its dependencies. The modules installed in the root package.json are installed in all the subproject. When you run lerna bootstrap it will run npm install on each project.
how i configure the lerna.json and package.json in the root folder.
The lerna.json is quite simple and contains configuration that collects all the project lerna should manage.
The package.json is to track shared packages between all your project. For example you could have some constant-module that must be the same version on both the projects.
In a simple fontend/backend, it is realistic to don't have any shared modules.
how can i run the application?
when you run lerna run start, lerna will execute npm run start on all the tracked project. So you must be aware how to start you whole application.
In my example, I implemented the start only in the backend project since it will serve statically the frontend files and I didn't want to start another webserver only for the frontend.
I've just started learning nodejs. I have my 'upper-case' module installed in C:\Users\User_name\node_modules directory and my nodejs project folder is in C:\wamp64\www.
So how can I write require() so that the 'upper-case' can be used in the project file.
Thanks in advance.
Well, you don't know the module system yet. It's a bad practice to install things globally (npm i -g package) or in an other directory.
Create a package.json in the base directory of your project (C:\wamp64\www) with "npm init" and install modules their (call "npm i module-name" in this directory). (Open the command line from this directory using the context menu or "cd" into it). Then you can simply use require('module-name')
Currently, if you are using a package.json file to manage your project's dependencies (whatever project it is, may it be a ruby, php, python or js app), by default everything is installed under ./node_modules.
When some dependencies have binaries to save, they're installed under ./node_modules/.bin.
What I need is a feature that allow me to change the ./node_modules/.bin directory for ./bin.
Simple example:
A PHP/Symfony app has a ./vendor dir for Composer dependencies, and all binaries are saved in ./bin, thanks to the config: { bin-dir: bin } option in composer.json.
But if I want to use Gulp to manage my assets, I create a package.json file, require all my dependencies and then run npm install.
Then, my wish is to run bin/gulp to execute gulp, but actually I have to run node_modules/.bin/gulp which is not as friendly as bin/gulp.
I've looked at package.json examples/guides on browsenpm.org and docs.npmjs.com, but none of them works, because they are here to define your own project's binaries. But I don't have any binaries, because I want to use binaries from other libraries.
Is there an option for that with NodeJS/NPM ?
You might consider adding gulp tasks to your package.json.
// package.json
{
"scripts": {
"build-templates": "gulp build-templates",
"minify-js": "gulp minify-js"
}
}
You can run any scripts specified in package.json by simply running the following:
$ npm run build-templates
$ npm run minify-js
You get the idea. You can use the gulp command inside the string without doing ./node_modules/.bin/gulp because npm is smart enough to put all scripts from ./node_modules/.bin/ into the path for that script execution.