Maybe I am totally off track here and I am chasing the wrong Idea:
I have node.js project which contains an npm module (let's call it my_npm_module) which I am also working on.
The module's directory is symlinked into my project like this:
/home
|
|__ myproject
| |
| |__ node_modules
| |__ some_other_module
| |__ my_npm_module (should only get installed/updated in production mode)
| |__ my_npm_module (symlinked only in development mode from /home/modules/mymodule)
|
|__modules
|__ my_npm_module (symlinked to /home/myproject/node_modules/mymodule)
Now, when I call npm install/update in development mode I don't want mymodule to get installed/updated, since I want to use the symlinked version.
But I want mymodule to get installed/updated in production mode. And only in production (NODE_ENV=production) mode.
Since package.json "dependencies" installs/updates for either mode i.e. development and production and "devDependencies" installs/updates for development mode only, I am kind of stuck here.
What I am looking for is something like "productionDependencies" or at least a solution which covers this situation.
Sorry, there is no way to do that.
What you could do is add an extra line when you deploy.
$ npm install my_module
$ NODE_ENV=production npm install
And when you finish with your module development ( let's say you don't want to link it anymore, because it's ready for production ), you just remove that extra line and add it to your dependencies.
Another idea is to modify your package.json file for production in your deploy script. This will ensure that the module will be installed only for production.
Related
I'm creating a codebase and from this codebase I want to build several tools: a CLI, and an API, all within the one repository as a mono-repo so I don't need to publish my package. What is the project structure convention in NodeJS projects to achieve this?
My current setup looks similar to this:
myProject/
- package.json
- src/
| - index.js
- packages/
| - cli/
| - api/
The CLI and API both consume src/index.js as an example. The root level package.json contains all the dependencies.
I'm not too experienced with NodeJS, what should be the best approach here? I think I'll need to give the API and CLI their own package.json files as they're technically separate from the main project package?
The situation:
client and server both share a folder shared
when we change shared in our development flow, we want the corresponding references to change in customer and server
server works because somehow with npm it seems to work, shared and server use npm
customer doesn't work and uses yarn
mixed typescript and js project
Code Structure:
root/
|- client/
|- package.json
|- src/
|- ...
|- server/
|- package.json
|- src/
|- ...
|- shared/
|- package.json // we don't want to change version every change
|- src/
|- ...
What's been tried
3 solutions proposed here
create a folder common under root and just require the files you need from your files. But you could end up with "long" require such as require("../../../../../common/file")
long require doesn't work with resolution with webpack - and isn't a nice solution
use module-alias to avoid that problem: https://github.com/ilearnio/module-alias
module-alias seems to be the same solution as the next one, in how it behaves.
you could make common a local module (using file:) and install it in package.json https://docs.npmjs.com/files/package.json#local-paths
we currently do this, but we still have to reinstall the shared folder on a change, we currently use yarn upgrade shared which takes a long time and still requires us to know when we need to run it.
in addition we've attempted to get yarn link working but doesn't seem to link properly
we've attempted to use a post-pull hook with husky to run yarn upgrade shared but it's too slow to run every pull especially since it's not needed often
we've considered a true mono-repo 'package' like Lerna but still don't think that it's necessary for the cost to migrate
link-module-alias to sym link folders on post install script, but this fails with typescript files
Goal
either
find a way to automatically sync these in dev environement
find a solution that installs/updates manually - but is fast and can be run on every pull
find a way to automate running yarn upgrade shared that runs (roughly) only when needed not all the time
I guess we could find a way to automate the version increment on any change of shared's version key, and then it's tracked, we could run yarn install and that work work.
The find solution was that we were using React-Native and therefore the normal steps for syncing would work for the IDE but not the React-Native app.
Here is a great article describing how to get metro bundler working with it - however for Typescript we added it to the TSConfig and for the IDE we still needed to add it to our package using the file:../shared directive.
I'm new in node world and I'm in facing with a problem,
Currently I'd made a split in the project (in ts) where I work in two new packages(using ts also), to be shared between applications.
All this code are in the same repo, popularly called "monorepo". Below I'll illustrate how this are joined.
But even using the npm link when we made the transpile, from this ts to js these code wasn't included in the final build, to solve this I created two npm packages.
Therefore, apparently this have been adding more complexity into development, because when we need change something in these packages, we need do a link between these package and the project and then if we made some new modification in the packages, it's needed run the npm build in these packages to update the source to be used, because I import the package transpiled (I have some issues regarding the es6 and your imports, node don't accepted it).
I would like how can I improve this method to be less bureaucratic and more effective.
To illustrate my current situation:
/Repo
|_ _/my-app
| |_ _/node_modules
| | |_ _/package-A (npm package)
| | |_ _/package-B (npm package)
|_ _/package-A
|_ _/package-B
|_ _/others-apps
13-10-2017 - Self answered, see below, problem solved
13-10-2017 - Update added, see end of question, still unsolved
Description first, problem later:
Our team uses a prepared development environment and I want to add the node components needed for building Liferay dxp themes with the liferay theme generator.
I've installed node from the Win64-Zip-Installation, so I just unpacked it to a custom directory (win10).
I've set the Environment Variables and created the .npmrc, the prefix (npm-packages) and cache (npm-cache) also reside in the above mentioned custom directory.
I've installed additional required components via npm (gulp, yo, generator-liferay-theme, liferay-theme-tasks, liferay-theme-deps-7.0, all with -g)
So my global installation looks like this
custom directory
|
+--- node-v6.11.4-win-x64
|
+--- npm-cache
|
+--- npm-packages
Environment variables are set
NODE_PATH=(custom directory)\node-v6.11.4-win-x64;(custom directory)\npm-packages\node_modules
NPM_PACKAGES=(custom directory)\npm-packages
Including the Path
Path=...;(custom directory)\node-v6.11.4-win-x64;(custom directory)\npm-packages
And the .npmrc goes like
prefix=(custom directory)\npm-packages
cache=(custom directory)\npm-cache
proxy=(our proxy)
registry=http://registry.npmjs.org/
fetch-retry-mintimeout=1000
fetch-retry-maxtimeout=3000
strict-ssl=false
Building the Liferay DXP theme works on my machine - WOMM
When I zip the complete custom folder and unzip it on another developer machine (and also setting environment variables and .npmrc on the other machine), the build doesn't work.
The error message (maven antrun exec gulp.cmd) is
[exec] Error: Cannot find module '(project directory)\node_modules\gulp\bin\gulp.js'
Where "(project directory)" is my DXP theme source folder, not the above mentioned custom installation folder.
Other commands show no problems on the other machine, like for example
node -v
npm -v
npm cache ls
Also a
npm install -g yo gulp
finished quickly and showed no error.
Update
One try yesterday was to call
npm install -g yo gulp
on the other machine, this should be the only difference to the content of the zipfile.
The call
npm list -g
shows these differences on the other machine
1099a1100
> +-- is-installed-globally#0.1.0 extraneous
1222a1224,1226
> | +-- chalk#2.1.0 invalid
> | | +-- ansi-styles#3.2.0 extraneous
> | | `-- supports-color#4.4.0 extraneous
The invalid entry is a child of yo.update-notifier
Next try on the other machine
npm uninstall -g yo
npm cache clear
npm install -g yo
The new diff between my and the other machine
1211c1211
< +-- update-notifier#2.2.0
---
> +-- update-notifier#2.3.0
1222a1223,1225
> | +-- chalk#2.1.0
> | | +-- ansi-styles#3.2.0
> | | `-- supports-color#4.4.0
1223a1227,1230
> | +-- is-installed-globally#0.1.0
> | | +-- global-dirs#0.1.0
> | | `-- is-path-inside#1.0.0
> | | `-- path-is-inside#1.0.2
1257c1264
< | +-- diff#3.3.1
---
> | +-- diff#3.4.0
But the build on the other machine still throws the same error.
Are you sure you have gulp in the development requirements?
Try - npm install gulp --save-dev
Self answering, some maven/antrun/jdk magic - I should bang my head against the wall multiple times.
When I started testing on my colleague's machine he mentioned "I'm on jdk6 because I'm developing this and that"
"No problem", I said, "it's not java".
But this was the problem. Switching to jdk8 made the build work on the colleague's machine.
Maybe I should have tested the command line build without maven, just "gulp build" on the prompt, because I guess, the jdk-problem is related to maven -> antrun/ant -> gulp and not to node and/or gulp itself (?)
Remembering the error when using jdk6:
[exec] Error: Cannot find module '(project directory)\node_modules\gulp\bin\gulp.js'
Summary
You can set up a zipfile containing global node installations and use it on other machines, just don't forget to
use no old java
set the environment variables and .npmrc content to match the directories you're using
Example
zip contains an unzipped node installation (win64 zip from nodejs for example)
zip contains node-packages and node-cache
unzip to c:\global-node
set NODE_PATH=c:\global-node\node-v6.11.4-win-x64;c:\global-node\npm-packages\node_modules
set NPM_PACKAGES=c:\global-node\npm-packages
set/extend path with ";c:\global-node\node-v6.11.4-win-x64;c:\global-node\npm-packages"
your ~/.npmrc needs an entry prefix=c:\global-node\npm-packages
and an entry cache=c:\global-node\npm-cache
And you're done with a reusable global zipped node installation.
I have a project in which I use node-webkit. node-webkit allows npm packages to be used for developing desktop applications. I make use of grunt to build my application.
My folder structure looks like this at the moment:
project root
node_modules/ (1)
package.json (1)
App/
node_modules/ (2)
package.json (2)
bower.json
bower_components/
...
controllers/
filters/
...
app.js
The npm dependencies for the application itself are kept within the App folder, but the dev dependencies for building the project are not related to the application source code, so i keep them in node_modules (1) inside the root folder. I also know that in a package.json file one can express dependencies and dev dependencies, exactly for this reason. I would rather have one package.json file in the root expressing ALL dependencies, including dev dependencies, but i would rather have a separation of those dependencies on folder level.
Two questions arise:
Is this a good way to organize my npm dependencies? If yes, awesome? If no, which I expect:
What is a better way to organize my dependencies? Is it possible to specify that dev dependencies go into folder a, and 'regular' dependencies go into folder b? If so, how do I do this?
In case anyone is wondering, this is the project i am talking about:
https://github.com/michahell/pinbored-webkit
[updated folder structure to include app.js for clarity]
It is perfectly fine to keep more than one package.json file and multiple node_module directories for a project. If you consider the parts as separate components.
An example might be if, you have one directory containing a node server, another containing a react app, and a third containing some kind of deployment script written in javascript.
#Michael package.json file contains all the dependencies related to that project.There is no need for multiple package files and multiple node_modules folders..
But you need to check where is your App.js file!!
your App.js , package.json must be in same folder unless configured.