Forked from branneman/better-nodejs-require-paths.md
Created
November 5, 2015 08:37
-
-
Save balduran/c739e92e8ca6a971652f to your computer and use it in GitHub Desktop.
Revisions
-
branneman revised this gist
May 25, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ Note: you can **not** have a symlink like this inside a Git repo, since Git does Alternatively, you can create the symlink on the npm `postinstall` hook, as described by [scharf](https://github.com/scharf) in [this awesome comment](https://gist.github.com/branneman/8048520#comment-1412502). Put this inside your `package.json`: ```js "scripts": { "postinstall" : "node -e \"var s='../app',d='node_modules/app',fs=require('fs');fs.exists(d,function(d){d||fs.symlinkSync(s,d,'dir')});\"" } ``` -
branneman revised this gist
May 25, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,7 +59,7 @@ Alternatively, you can create the symlink on the npm `postinstall` hook, as desc 3. In your very/far/away/module.js: ```js var Article = require('models/article'); ``` ### 4. The Environment -
branneman revised this gist
May 25, 2015 . 1 changed file with 4 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -47,13 +47,13 @@ Alternatively, you can create the symlink on the npm `postinstall` hook, as desc 1. Install some module: ```sh npm install app-module-path --save ``` 2. In your app.js, before any `require()` calls: ```js require('app-module-path').addPath(__dirname + '/app'); ``` 3. In your very/far/away/module.js: @@ -62,18 +62,6 @@ Alternatively, you can create the symlink on the npm `postinstall` hook, as desc var Article = rekuire('models/article'); ``` ### 4. The Environment Set the `NODE_PATH` environment variable to the absolute path of your application, ending with the directory you want your modules relative to (in my case `.`). @@ -161,7 +149,7 @@ If you're using CVS or SVN (but not Git!), this solution is a great one which wo You're effectivly swapping `../../../` for `__base +` which is only slightly better if you ask me. However it's very obvious for the next developer what's exactly happening. That's a big plus compared to the other *magical* solutions around here. **3. The Module** Great and simple solution. Does not touch other require calls to `node_modules`. **4. The Environment** Setting application-specific settings as environment variables globally or in your current shell is an anti-pattern if you ask me. E.g. it's not very handy for development machines which need to run multiple applications. -
branneman revised this gist
May 25, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -148,7 +148,7 @@ You can then require modules like this: var Article = rootRequire('app/models/article'); ``` Another option is to always use the initial `require()` function, basically the same trick without a wrapper. Node.js creates a new scoped `require()` function for every new module, but there's always a reference to the initial global one. Unlike most other solutions this is actually a documented feature. It can be used like this: ```js var Article = require.main.require('app/models/article'); ``` -
branneman revised this gist
Mar 30, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -100,7 +100,7 @@ Windows: `cmd.exe /C "set NODE_PATH=.&& node app"` (On Windows this command will **not** work if you put a space in between the path and the `&&`. Crazy shit.) ### 5. The Start-up Script Effectively, this solution also uses the environment (as in 4.2), it just abstracts it away. With one of these solutions (5.1 & 5.2) you can start your application like this from now on: Linux: `./app` _(also for Windows PowerShell)_ -
branneman revised this gist
Mar 30, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ Windows: `mklink /D app node_modules` Note: you can **not** have a symlink like this inside a Git repo, since Git does not handle symlinks cross-platform. If you can live with a post-clone git-hook and/or the instruction for the next developer to create a symlink, then sure. Alternatively, you can create the symlink on the npm `postinstall` hook, as described by [scharf](https://github.com/scharf) in [this awesome comment](https://gist.github.com/branneman/8048520#comment-1412502). Put this inside your `package.json`: ```js "scripts": { "postinstall" : "node -e \"var s='../app',d='node_modules/app',fs=require('fs');fs.exists(d,function(d){d||fs.symlinkSync(s,dstpath,'dir')});\"" -
branneman revised this gist
Mar 30, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ Note: you can **not** have a symlink like this inside a Git repo, since Git does Alternatively, you can create the symlink on the npm `postinstall` hook, as described by [scharf]() in [this awesome comment](https://gist.github.com/branneman/8048520#comment-1412502). Put this inside your `package.json`: ```js "scripts": { "postinstall" : "node -e \"var s='../app',d='node_modules/app',fs=require('fs');fs.exists(d,function(d){d||fs.symlinkSync(s,dstpath,'dir')});\"" } ``` -
branneman revised this gist
Mar 30, 2015 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,6 +24,13 @@ Windows: `mklink /D app node_modules` Note: you can **not** have a symlink like this inside a Git repo, since Git does not handle symlinks cross-platform. If you can live with a post-clone git-hook and/or the instruction for the next developer to create a symlink, then sure. Alternatively, you can create the symlink on the npm `postinstall` hook, as described by [scharf]() in [this awesome comment](https://gist.github.com/branneman/8048520#comment-1412502). Put this inside your `package.json`: ```js "scripts": { "postinstall" : "node -e \"var srcpath='../app',dstpath='node_modules/app',fs=require('fs');fs.exists(dstpath,function(s){s||fs.symlinkSync(srcpath,dstpath,'dir')});\"" } ``` ### 2. The Global 1. In your app.js: ```js -
branneman revised this gist
Nov 8, 2014 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -141,6 +141,11 @@ You can then require modules like this: var Article = rootRequire('app/models/article'); ``` Another option is to always use the initial `require()` function, basically the same trick without a wrapper. Node.js creates a new scoped `require()` function for every new module, but you there's always a reference to the initial global one. Unlike most other solutions this is actually a documented feature. It can be used like this: ```js var Article = require.main.require('app/models/article'); ``` ## Conclusion **1. The Symlink** If you're using CVS or SVN (but not Git!), this solution is a great one which works, otherwise I don't recommend this to anyone. @@ -163,4 +168,4 @@ You're simplifying the command to start your app (always simply `node app`), and Most simple solution of all. Use at your own risk. **7. The Wrapper** Great and non-hacky solution. Very obvious what it does, especially if you pick the `require.main.require()` one. -
branneman revised this gist
Nov 8, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -121,7 +121,7 @@ Courtesy of [@joelabair](https://github.com/joelabair). Effectively also the sam This code needs to be placed in your `app.js`, before any require() calls: ```js process.env.NODE_PATH = __dirname; require('module').Module._initPaths(); ``` -
branneman revised this gist
Jul 17, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -126,7 +126,7 @@ require('module').Module._initPaths(); ``` ### 7. The Wrapper Courtesy of [@a-ignatov-parc](https://github.com/a-ignatov-parc). Another simple solution which increases obviousness, simply wrap the `require()` function with one relative to the path of the application's entry point file. Place this code in your `app.js`, again before any require() calls: -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,8 +7,6 @@ var Article = require('../../../models/article'); ``` Those suck for maintenance and they're ugly. ## Possible solutions Ideally, I'd like to have the same basepath from which I `require()` all my modules. Like any other language environment out there. I'd like the `require()` calls to be first-and-foremost relative to my application entry point file, in my case `app.js`. -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ var Article = require('../../../models/article'); ``` Those suck for maintenance and they're ugly. **TL;DR**: I *personally* like solution **5.1** the best, but they all work. ## Possible solutions Ideally, I'd like to have the same basepath from which I `require()` all my modules. Like any other language environment out there. I'd like the `require()` calls to be first-and-foremost relative to my application entry point file, in my case `app.js`. -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 7 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,11 +7,11 @@ var Article = require('../../../models/article'); ``` Those suck for maintenance and they're ugly. **TL;DR**: I only really like solutions **5.1** and **6**, but they all work. ## Possible solutions Ideally, I'd like to have the same basepath from which I `require()` all my modules. Like any other language environment out there. I'd like the `require()` calls to be first-and-foremost relative to my application entry point file, in my case `app.js`. ### 1. The Symlink Stolen from: [focusaurus](https://github.com/focusaurus) / [express_code_structure](https://github.com/focusaurus/express_code_structure) # [the-app-symlink-trick](https://github.com/focusaurus/express_code_structure#the-app-symlink-trick) @@ -159,7 +159,10 @@ Setting application-specific settings as environment variables globally or in yo If you're adding it only for the currently executing program, you're going to have to specify it each time you run your app. Your start-app command is not easy anymore, which also sucks. **5. The Start-up Script** You're simplifying the command to start your app (always simply `node app`), and it gives you a nice spot to put your mandatory v8 parameters! A small disadvantage might be that you need to create a seperate start-up script for your unit tests as well. **6. The Hack** Most simple solution of all. Use at your own risk. **7. The Wrapper** Great and non-hacky solution. Very obvious what it does. -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 16 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -127,6 +127,22 @@ process.env.NODE_PATH = '.'; require('module').Module._initPaths(); ``` ### 7. The Wrapper Courtesy of [@a-ignatov-parc](https://github.com/a-ignatov-parc). Another simple solution which increases oviousness, simply wrap the `require()` function with one relative to the path of the application's entry point file. Place this code in your `app.js`, again before any require() calls: ```js global.rootRequire = function(name) { return require(__dirname + '/' + name); } ``` You can then require modules like this: ```js var Article = rootRequire('app/models/article'); ``` ## Conclusion **1. The Symlink** If you're using CVS or SVN (but not Git!), this solution is a great one which works, otherwise I don't recommend this to anyone. -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ # Better local require() paths for Node.js ## Problem When the directory structure of your Node.js **application** (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like: ```js var Article = require('../../../models/article'); ``` -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -132,7 +132,7 @@ require('module').Module._initPaths(); If you're using CVS or SVN (but not Git!), this solution is a great one which works, otherwise I don't recommend this to anyone. **2. The Global** You're effectivly swapping `../../../` for `__base +` which is only slightly better if you ask me. However it's very obvious for the next developer what's exactly happening. That's a big plus compared to the other *magical* solutions around here. **3. The Module** The verdict for this one is the same as for solution 2, this is a very nasty hack. Steer clear. -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ Those suck for maintenance and they're ugly. ## Possible solutions Ideally, I'd like to have the same basepath from which I `require()` all my modules. Like any other language environment out there. I'd like the `require()` calls to be first-and-foremost relative to my application entry point file, in my case `app.js`. **TL;DR Warning**: I only really like solutions **2**, **5.1** and **6**, but they all work. ### 1. The Symlink Stolen from: [focusaurus](https://github.com/focusaurus) / [express_code_structure](https://github.com/focusaurus/express_code_structure) # [the-app-symlink-trick](https://github.com/focusaurus/express_code_structure#the-app-symlink-trick) @@ -132,7 +132,7 @@ require('module').Module._initPaths(); If you're using CVS or SVN (but not Git!), this solution is a great one which works, otherwise I don't recommend this to anyone. **2. The Global** You're effectivly swapping `../../../` for `__base +` which is only very slightly better if you ask me. It does make very clear to the next developer what's happening here. That's a big plus compared to the other *magical* solutions around here. **3. The Module** The verdict for this one is the same as for solution 2, this is a very nasty hack. Steer clear. -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -123,7 +123,7 @@ Courtesy of [@joelabair](https://github.com/joelabair). Effectively also the sam This code needs to be placed in your `app.js`, before any require() calls: ```js process.env.NODE_PATH = '.'; require('module').Module._initPaths(); ``` -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ Those suck for maintenance and they're ugly. ## Possible solutions Ideally, I'd like to have the same basepath from which I `require()` all my modules. Like any other language environment out there. I'd like the `require()` calls to be first-and-foremost relative to my application entry point file, in my case `app.js`. **TL;DR Warning**: I only really like solutions **5.1** and **6**, but they all work. ### 1. The Symlink Stolen from: [focusaurus](https://github.com/focusaurus) / [express_code_structure](https://github.com/focusaurus/express_code_structure) # [the-app-symlink-trick](https://github.com/focusaurus/express_code_structure#the-app-symlink-trick) @@ -29,13 +29,13 @@ Note: you can **not** have a symlink like this inside a Git repo, since Git does ### 2. The Global 1. In your app.js: ```js global.__base = __dirname + '/'; ``` 2. In your very/far/away/module.js: ```js var Article = require(__base + 'app/models/article'); ``` ### 3. The Module @@ -70,11 +70,11 @@ var myMod = rekuire('app/a/b/c/d/e/f/my-mod.js'); Just so you know, rekuire actually handles ambiguity the right way, with a sane error message. ### 4. The Environment Set the `NODE_PATH` environment variable to the absolute path of your application, ending with the directory you want your modules relative to (in my case `.`). There are 2 ways of achieving the following `require()` statement from anywhere in your application: ```js var Article = require('app/models/article'); ``` #### 4.1. Up-front @@ -119,7 +119,7 @@ cmd.exe /C "set NODE_PATH=.&& node app.js" ``` ### 6. The Hack Courtesy of [@joelabair](https://github.com/joelabair). Effectively also the same as 4.2, but without the need to specify the `NODE_PATH` outside your application, making it more fool proof. However, since this relies on a private Node.js core method, this is also a hack that might stop working on the previous or next version of node. This code needs to be placed in your `app.js`, before any require() calls: ```js @@ -146,4 +146,4 @@ If you're adding it only for the currently executing program, you're going to ha I'd go for this one (5.1)! Apart from 1-2 (tiny) extra files, no disadvantages here. You're simplifying the command to start your app, and it gives you a nice spot to put your mandatory v8 parameters! **6. The Hack** Most simple solution of all. Use at your own risk. -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 28 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,39 +19,43 @@ Stolen from: [focusaurus](https://github.com/focusaurus) / [express_code_structu Linux: `ln -nsf node_modules app` Windows: `mklink /D app node_modules` 2. Now you can require local modules like this from anywhere: ```js var Article = require('models/article'); ``` Note: you can **not** have a symlink like this inside a Git repo, since Git does not handle symlinks cross-platform. If you can live with a post-clone git-hook and/or the instruction for the next developer to create a symlink, then sure. ### 2. The Global 1. In your app.js: ```js global.__base = __dirname + '/app/'; ``` 2. In your very/far/away/module.js: ```js var Article = require(__base + 'models/article'); ``` ### 3. The Module 1. Install some module: ```sh npm install rekuire ``` 2. In your app.js: ```js global.rekuire = require('rekuire'); ``` 3. In your very/far/away/module.js: ```js var Article = rekuire('models/article'); ``` Mind that [rekuire](https://github.com/nadav-dav/rekuire) will allow you to not specify any path at all, so the file `app/a/b/c/d/e/f/my-mod.js` can just be loaded like this: ```js -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -115,7 +115,7 @@ cmd.exe /C "set NODE_PATH=.&& node app.js" ``` ### 6. The Hack Courtesy of [@joelabair](https://github.com/joelabair). This one also makes use of the `NODE_PATH` variable, but without the need to specify this outside the application, making it more fool proof. However, since this relies on a private Node.js core method, this is also a hack that might stop working on the next version of node. This code needs to be placed in your `app.js`, before any require() calls: ```js @@ -139,4 +139,7 @@ Setting application-specific settings as environment variables globally or in yo If you're adding it only for the currently executing program, you're going to have to specify it each time you run your app. Your start-app command is not easy anymore, which also sucks. **5. The Start-up Script** I'd go for this one (5.1)! Apart from 1-2 (tiny) extra files, no disadvantages here. You're simplifying the command to start your app, and it gives you a nice spot to put your mandatory v8 parameters! **6. The Hack** Most simple solution of all, but it's hack. -
branneman revised this gist
Jun 30, 2014 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,7 @@ global.__base = __dirname + '/app/'; var Article = require(__base + 'models/article'); ``` ### 3. The Module 1. Install some module: ```sh npm install rekuire @@ -114,6 +114,15 @@ Windows, create `app.bat` in your project root: cmd.exe /C "set NODE_PATH=.&& node app.js" ``` ### 6. The Hack Courtesy of @joelabair. This one also makes use of the `NODE_PATH` variable, but without the need to specify this outside the application, making it more fool proof. However, since this relies on a private Node.js core method, this is also a hack that might stop working on the next version of node. This code needs to be placed in your `app.js`, before any require() calls: ```js process.env['NODE_PATH'] = '.'; require('module').Module._initPaths(); ``` ## Conclusion **1. The Symlink** If you're using CVS or SVN (but not Git!), this solution is a great one which works, otherwise I don't recommend this to anyone. -
branneman revised this gist
Feb 3, 2014 . 1 changed file with 11 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -93,6 +93,16 @@ Windows: `cmd.exe /C "set NODE_PATH=.&& node app"` ### 5. The Start-up Script Effectivly, this solution also uses the environment (as in 4.2), it just abstracts it away. With one of these solutions (5.1 & 5.2) you can start your application like this from now on: Linux: `./app` _(also for Windows PowerShell)_ Windows: `app` An advantage of this solution is that if you want to force your node app to always be started with v8 parameters like `--harmony` or `--use_strict`, you can easily add them in the start-up script as well. #### 5.1. Node.js Example implementation: [https://gist.github.com/branneman/8775568](https://gist.github.com/branneman/8775568) #### 5.2. OS-specific start-up scripts Linux, create `app.sh` in your project root: ```sh #!/bin/sh @@ -103,11 +113,6 @@ Windows, create `app.bat` in your project root: @echo off cmd.exe /C "set NODE_PATH=.&& node app.js" ``` ## Conclusion **1. The Symlink** @@ -125,4 +130,4 @@ Setting application-specific settings as environment variables globally or in yo If you're adding it only for the currently executing program, you're going to have to specify it each time you run your app. Your start-app command is not easy anymore, which also sucks. **5. The Start-up Script** I'd go for this one (5.1)! Apart from 1-2 (tiny) extra files, no disadvantages here. You're simplifying the command to start your app, and it gives you a nice spot to put your mandatory v8 parameters! -
branneman revised this gist
Jan 6, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -125,4 +125,4 @@ Setting application-specific settings as environment variables globally or in yo If you're adding it only for the currently executing program, you're going to have to specify it each time you run your app. Your start-app command is not easy anymore, which also sucks. **5. The Start-up Script** I'd go for this one! Apart from 2 (tiny) extra files, no disadvantages here. You're simplifying the command to start your app, and it gives you a nice spot to put your mandatory v8 parameters! -
branneman revised this gist
Jan 6, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -125,4 +125,4 @@ Setting application-specific settings as environment variables globally or in yo If you're adding it only for the currently executing program, you're going to have to specify it each time you run your app. Your start-app command is not easy anymore, which also sucks. **5. The Start-up Script** I'd go for this one! Apart from 2 (tiny) extra files, no disadvantages here. You're even simplifying the command to start your app, and it even gives you a nice spot to put your mandatory v8 parameters! -
branneman revised this gist
Jan 5, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -90,7 +90,7 @@ Windows: `cmd.exe /C "set NODE_PATH=.&& node app"` (On Windows this command will **not** work if you put a space in between the path and the `&&`. Crazy shit.) ### 5. The Start-up Script Effectivly, this solution also uses the environment (as in 4.2), it just abstracts it away. Linux, create `app.sh` in your project root: @@ -116,13 +116,13 @@ If you're using CVS or SVN (but not Git!), this solution is a great one which wo **2. The Global** You're effectivly swapping `../../../` for `__base +` which is only very slightly better if you ask me. It saves you from thinking about paths, but adds dirty bloat. Ugh. **3. The Module** The verdict for this one is the same as for solution 2, this is a very nasty hack. Steer clear. **4. The Environment** Setting application-specific settings as environment variables globally or in your current shell is an anti-pattern if you ask me. E.g. it's not very handy for development machines which need to run multiple applications. If you're adding it only for the currently executing program, you're going to have to specify it each time you run your app. Your start-app command is not easy anymore, which also sucks. **5. The Start-up Script** I'd go for this one! Apart from 2 (tiny) extra files, no disadvtanges here. You're even simplifying the command to start your app, and it even gives you a nice spot to put your mandatory v8 parameters! -
branneman revised this gist
Jan 5, 2014 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -43,9 +43,13 @@ var Article = require(__base + 'models/article'); npm install rekuire ``` 2. In your app.js: ```js global.rekuire = require('rekuire'); ``` 3. In your very/far/away/module.js: ```js var Article = rekuire('models/article'); ``` -
branneman revised this gist
Jan 5, 2014 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -106,19 +106,19 @@ Windows: `app` An advantage of this solution is that if you want to force your node app to always be started with v8 parameters like `--harmony` or `--use_strict`, you can easily add them in the start-up script as well. ## Conclusion **1. The Symlink** If you're using CVS or SVN (but not Git!), this solution is a great one which works, otherwise I don't recommend this to anyone. **2. The Global** You're effectivly swapping `../../../` for `__base +` which is only very slightly better if you ask me. It saves you from thinking about paths, but adds dirty bloat. Ugh. **3. The module** The verdict for this one is the same as for solution 2, this is a very nasty hack. Steer clear. **4. The Environment** Setting application-specific settings as environment variables globally or in your current shell is an anti-pattern if you ask me. E.g. it's not very handy for development machines which need to run multiple applications. If you're adding it only for the currently executing program, you're going to have to specify it each time you run your app. Your start-app command is not easy anymore, which also sucks. **5. The start-up script** I'd go for this one! Apart from 2 (tiny) extra files, no disadvtanges here. You're even simplifying the command to start your app, and it even gives you a nice spot to put your mandatory v8 parameters! -
branneman revised this gist
Jan 5, 2014 . 1 changed file with 42 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,23 +37,47 @@ global.__base = __dirname + '/app/'; var Article = require(__base + 'models/article'); ``` ### 3. The module 1. Install some module: ```sh npm install rekuire ``` 2. Anywhere in your any application: ```js var rekuire = require('rekuire'); var Article = rekuire('models/article'); ``` Mind that [rekuire](https://github.com/nadav-dav/rekuire) will allow you to not specify any path at all, so the file `app/a/b/c/d/e/f/my-mod.js` can just be loaded like this: ```js var myMod = rekuire('my-mod'); ``` If you choose rekuire, I advise you to make all paths relative to your app/ directory, to prevent any ambiguity. Like this: ```js var myMod = rekuire('app/a/b/c/d/e/f/my-mod.js'); ``` Just so you know, rekuire actually handles ambiguity the right way, with a sane error message. ### 4. The Environment Set the `NODE_PATH` environment variable to the absolute path of your application, ending with the directory you want your modules relative to (in my case `app/`). There are 2 ways of achieving the following `require()` statement from anywhere in your application: ```js var Article = require('models/article'); ``` #### 4.1. Up-front Before running your `node app`, first run: Linux: `export NODE_PATH=.` Windows: `set NODE_PATH=.` Setting a variable like this with `export` or `set` will remain in your environment as long as your current shell is open. To have it globally available in any shell, set it in your userprofile and reload your environment. #### 4.2. Only while executing node This solution will not affect your environment other than what node preceives. It does change your application start command. Start your application like this from now on: @@ -62,8 +86,8 @@ Windows: `cmd.exe /C "set NODE_PATH=.&& node app"` (On Windows this command will **not** work if you put a space in between the path and the `&&`. Crazy shit.) ### 5. The start-up script Effectivly, this solution also uses the environment (as in 4.2), it just abstracts it away. Linux, create `app.sh` in your project root: ```sh @@ -81,26 +105,20 @@ Windows: `app` An advantage of this solution is that if you want to force your node app to always be started with v8 parameters like `--harmony` or `--use_strict`, you can easily add them in the start-up script as well. ## Conclusion ** 1. The Symlink** If you're using CVS or SVN (but not Git!), this solution is a great one which works, otherwise I don't recommend this to anyone. ** 2. The Global** You're effectivly swapping `../../../` for `__base +` which is only very slightly better if you ask me. It saves you from thinking about paths, but adds dirty bloat. Ugh. ** 3. The module** The verdict for this one is the same as for solution 2, this is a very nasty hack. Steer clear. ** 4. The Environment** Setting application-specific settings as environment variables globally or in your current shell is an anti-pattern if you ask me. E.g. it's not very handy for development machines which need to run multiple applications. If you're adding it only for the currently executing program, you're going to have to specify it each time you run your app. Your start-app command is not easy anymore, which also sucks. ** 5. The start-up script** I'd go for this one! Apart from 2 (tiny) extra files, no disadvtanges here. You're even simplifying the command to start your app, and it even gives you a nice spot to put your mandatory v8 parameters!
NewerOlder