I'm currently working on some university coursework, which is essentially an API written in express. We're allowed to use other packages as long as we follow the spec.
I'm trying to use consign for autoloading my models, middleware and routes. The problem I'm facing is the folder structure that we have to follow.
coursework
|-- artifact
| |-- server
| | |-- api
| | | |-- models
| | | | `-- stories.js
| | | |-- middleware
| | | | `-- stories.js
| | | `-- routers
| | | `-- stories.js
| | |-- node_modules
| | |-- package.json
| | |-- package-lock.json
| | `-- server.js
| `-- utilities
|-- node_modules
|-- webpages
|-- package.json
|-- package-lock.json
`-- test.js
Inside of artifact is where all of our code goes, nothing else is touched. The root directory that contains test.js is where we will be assessed. From the root we run npm test (QUnit) and then it lists which tests have passed and which have failed.
The problem is we have to start our server from the root directory by calling node artifact/server which is fine because it calls artifact/server/server.js.
Inside of artifact/server/server.js I have the following code to initialise consign:
consign({ cwd: './artifact/server/api' })
.include('models')
.then('middleware')
.then('routers')
.into(app);
When I run node artifact/server from the root directory I get the following error:
What's frustrating is if I change consign's cwd property to api and then start the server inside of artifact/server by calling node server.js it works perfectly fine.
The problem is I need to be able to start the server from the root directory. I'm completely stuck as I do not know how I can modify consign to work from the root directory.
Any ideas?
I managed to find a solution to my problem and felt that I should post an answer to help others.
Essentially, as shown above I was using a relative path like so:
consign({ cwd: './artifact/server/api' })
.include('models')
.then('middleware')
.then('routers')
.into(app);
which when called from the root directory resulted in consign not working. This can be fixed by making use of path
I added this at the top of my server.js file:
const path = require('path');
and then changed my consign init to:
consign({ cwd: path.join(__dirname, 'api') })
.include('models')
.then('middleware')
.then('routers')
.into(app);
This then fixed my issue!
I was running into the same issue and getting this error
! Entity not found
...but then read Node's path documentation and tried the following:
consign({
cwd: path.dirname(__dirname)
})
Seems to be a solution if you need the working directory cwd: to be root './'
Related
I want to initialize a monorepo, and then initialize apps inside the packages folder. I found a bunch of examples of how to do this. But, what I want to achieve is, I want to keep all the node_modules at the root level.
--monorepo
|--node_modules
|--packages
| |--create-react-app-1
| | |--src
| | |--{...rest}
| |--create-react-app-2
| | |--src
| | |--{...rest}
|--package.json
So, all the node_modules are at the root level and all the packages are using them from the root level. All the dependencies and dev-dependencies are listed in the root level package.json.
I want to use yarn as the package manager as well.
How I can achieve this?
So I want to run ng test and include tests in a folder next to the projectfolder.
mainproject/
|-- src/
| |-- app/
| | |-- home/
| | | |-- home.component.spec.ts
| | |-- app-main.component.spec.ts
| |-- test.ts
| |-- tsconfig.spec.json
|-- node_modules/
|-- angular.json
|-- package.json
|-- tsconfig.json
components/
|-- common/
|-- control-bar/
|-- control-bar.component.ts
|-- control-bar.component.spec.ts
When I run ng test without including the other tests in the components folder it works as intended, but when I try to include the other components ng test fails with the following error.
ERROR in ../components/common/control-bar/control-bar.component.ts
Module not found: Error: Can't resolve 'raw-loader' in 'absolutepath\repos\components\common\control-bar'
resolve 'raw-loader' in 'absolutepath\repos\components\common\control-bar'
Parsed request is a module
No description file found
resolve as module
absolutepath\repos\components\common\control-bar\node_modules doesn't exist or is not a directory
absolutepath\repos\components\common\node_modules doesn't exist or is not a directory
absolutepath\repos\components\node_modules doesn't exist or is not a directory
absolutepath\repos\node_modules doesn't exist or is not a directory
absolutepath\node_modules doesn't exist or is not a directory
C:\Users\user\Desktop\node_modules doesn't exist or is not a directory
C:\Users\user\node_modules doesn't exist or is not a directory
C:\Users\node_modules doesn't exist or is not a directory
C:\node_modules doesn't exist or is not a directory
looking for modules in absolutepath\repos\mainproject\node_modules\#angular-devkit\build-angular\node_modules
using description file: absolutepath\repos\mainproject\node_modules\#angular-devkit\build-angular\package.json (relative path: ./node_modules)
using description file: C:\Users\user\Desktop\folder absolutepath\repos\mainproject\node_modules\#angular-devkit\build-angular\package.json (relative path: ./node_modules/raw-loader)
no extension
absolutepath\repos\mainproject\node_modules\#angular-devkit\build-angular\node_modules\raw-loader doesn't exist
.js
absolutepath\repos\mainproject\node_modules\#angular-devkit\build-angular\node_modules\raw-loader.js doesn't exist
.json
absolutepath\repos\mainproject\node_modules\#angular-devkit\build-angular\node_modules\raw-loader.json doesn't exist
as directory
absolutepath\repos\mainproject\node_modules\#angular-devkit\build-angular\node_modules\raw-loader doesn't exist
[C:\Users\user\Desktop\folder\repos\components\common\control-bar\package.json]
[C:\Users\user\Desktop\folder\repos\components\common\control-bar\node_modules]
[C:\Users\user\Desktop\folder\repos\components\common\node_modules]
[C:\Users\user\Desktop\folder\repos\components\node_modules]
[C:\Users\user\Desktop\folder\repos\node_modules]
[C:\Users\user\Desktop\folder\node_modules]
[C:\Users\user\Desktop\node_modules]
[C:\Users\user\node_modules]
[C:\Users\node_modules]
[C:\node_modules]
[C:\Users\user\Desktop\folder\repos\mainproject\node_modules\#angular-devkit\build-angular\node_modules\raw-loader]
[C:\Users\user\Desktop\folder\repos\mainproject\node_modules\#angular-devkit\build-angular\node_modules\raw-loader.js]
[C:\Users\user\Desktop\folder\repos\mainproject\node_modules\#angular-devkit\build-angular\node_modules\raw-loader.json]
# ../components/common/control-bar/control-bar.component.ts 13:34-85
# ../components/common/control-bar/control-bar.component.spec.ts
# ./src/test.ts
So raw-loader seems to be missing or it can't find it. To be clear there is only 1 node_modules in this setup and it's inside the mainproject folder.
I'm having the exact same issue and I've abandoned the search for a proper solution. For now I'm making it work by making sure the dependencies of the library are installed separately (so having a second node_modules folder in the library).
Since there is nothing to be found on the subject and the official angular documentation for creating libraries mentions that it is required for libraries to be built:
Build the library. You cannot use a library before it is built.
I'm assuming dynamically linking to unbuilt libraries without its own node_modules is simply unsupported (even though it works fine as long as there are no component classes imported in the library).
I've a folder structure for my project that looks roughly like this:
myproject
|-- some-folder
| `-- src
| `-- index.tsx
|-- src
| |-- components
| | `-- files.tsx
| `-- index.tsx
`-- webpack.config.js
When I run webpack, it tries to bundle the index.tsx file in both src and some-folder.
This would then throw an error because the index.tsx file in some-folder has certain dependencies webpack cannot resolve since is not meant to be bundled together.
This is how I've configured my webpack's entry point:
var config = {
context: __dirname + '/src',
entry: {
app: './index.tsx'
}
}
How can I configure webpack to only look for files within the src folder and not else where unless I define another different full path in the entry point?
Seeing that this is pretty old, it might be solved already, but was it something to do with adding an "exclude" statement to make sure the "/some-folder" was omitted?
I am building a Node app that returns results for a search using a Google Custom Search Engine(CSE).
I am going to separate the part of the app that sends the request to Google and returns the results into a module.
I use dotenv already in the app to store MongoDB credentials and the app's URL.
I also want to use dotenv in the module to store the Google CSE ID and the API key for the CSE.
I want my module to work independently of the main app but also to use the main app's dotenv file when it's a module.
Currently my module structure looks like this:
module
|
+-- node_modules
| |
| \-- dotenv
| |
| \-- (dotenv module's files....)
|
+-- .env
|
\-- index.js
This works perfectly on its own. The .env file stores the required environment variables and I can access them in the index.js file by requiring the dotenv module.
When included in the main app the structure looks like this:
app
|
+-- node_modules
| |
| +-- dotenv
| | |
| | \-- (dotenv module's files....)
| |
| \-- my_google_search_module
| |
| +-- node_modules
| | |
| | +-- dotenv
| | |
| | \-- (dotenv module's files...)
| |
| \-- index.js
|
+-- .env
|
\-- index.js
This also works. I store all the environment variables in the main app's .env file and by requiring dotenv in the app's index.js I can access those variables. Plus, the "my_google_search_module" seems to be pulling its required variables from the .env file in the root of the app. There is no .env file in the module.
My question is am I doing this the right way?
I have researched this further and can confirm that the module's .env is pulling the required environment variables from the app's .env file.
I believe this section from the dotenv readme, though not exactly related, verifies that -
https://www.npmjs.com/package/dotenv#what-happens-to-environment-variables-that-were-already-set
We will never modify any environment variables that have already been
set. In particular, if there is a variable in your .env file which
collides with one that already exists in your environment, then that
variable will be skipped. This behavior allows you to override all
.env configurations with a machine-specific environment, although it
is not recommended.
Yes, you are doing in a right way. There must be a single .env file in a whole project. But there is a trick to include it in the different directory structure.
For example:
Your index.js file is in /app/src, your .env file is in /app. Your index.js file has this
dotenv.config({path: "../.env"});
You could also use dotenv.config({path: path.join(__dirname, "../.env")});
for node projects, i would suggest to use npm package dotenv. You can find details on how to use it. do not forget to include require('dotenv').config() at the start of your project file, say index.js.
Now you can use .env contents anywhere you need. For example i want my server port to be 4000 which i define in .env as PORT=4000. Now, to use .env variables anywhere, simply provide variable name in suffix such as process.env.PORT. That is it. Though i am late on this post, hope this could be of any help.
I'm staring to build a project in node. The directory structure I've decided to use is to have all my source files under ./src, and the files I'll eventually upload to the server under ./dist. The (not quite complete) directory structure is shown below. The ./dist structure will eventually mirror ./src once it's built.
|-- LICENSE
|-- Procfile
|-- dist
| `-- server.js
|-- gulpfile.js
|-- package.json
|-- scripts
|-- src
| |-- assets
| | |-- fonts
| | `-- images
| |-- common
| | |-- directives
| | |-- resources
| | `-- services
| | `-- authentication
| |-- index.jade
| |-- less
| |-- server
| `-- server.js
|-- test
| `-- server.spec.js
`-- vendor
My question is, when I'm setting the view folders for ./src/server.js, should I set them to the files under ./dist, or ./src? I see reasons for both, but it doesn't seem like anybody would do that.
serving from ./dist makes the most sense for production, obviously, since this will be the only folder shipped to the production server. Right?
serving from ./src will allow me to test the server and its pages before building/minifying/etc. the entire thing into ./dist.
How do people separate the built files from the source while also being able to test and see Express views from the source? Is it as simple as setting views under the conditional process.env.NODE_ENV === "production"? Is there a more elegant solution-- to either my server app code or directory structure?
for the view directory, I advice to put it in the src folder and just copy them with a gulp task