I have two libraries in a nestJS monorepo:
Library1
Library2
I have one or more applications:
App1
App2
Both libraries use the '#app' namespace.
I can use either Library1 or Library2 in either App1 or App2 by importing them:
App1:
import { Library1 } from '#app/library1';
#Module({
imports: [Library1, Library2]
// ...
});
But what I need to do is import Library2 into Library1. This doesn't appear to work:
Cannot find module '#app/library2' or its corresponding type declarations
How would I go about doing this? I can't see anything online about it
Related
I have this piece of structure in my nestjs monorepo:
apps
|-project1
|-src
|-admin
|-dtos (class 1, class2... index.ts)
|-project2
|-src
|-user
|-class 3 (import {class1} from 'xxx'
Attempt 1 - I let it auto import and xxx be like: "#apps/project1/src/admin/dtos".
Attempt 2 - I using relative path and xxx be like: "../../../project1/src/admin/dtos".
Both attemps failed because when I build the two projects, the project 2 built contains like:
dist
|-project1
|-project2
|-project1
|-project2
And the projects can not start because the terminal says: "Can not find module xxx in project1".
How can I resolve this one. I have tried to search a lot but seems like I can not solve it. FYI: I am using nestjs cli to create monorepo. Thanks in advance!
You can use the Nest CLI to create a library (for this example I use the prefix libs when prompted by the command below):
nest g library my-library
The files
index.ts
my-library.module.ts
my-library.service.ts
will be generated in libs/my-library/src. You just need to keep index.ts in this directory and remove the other files.
Afterwards you can create files for your classes in libs/my-library/src, for example:
export class MyOwnDTO {}
Then you need to add an export to your index.ts in your libs/my-library/src:
export { MyOwnDTO } from './myown.dto';
After doing this you can import your DTO in your apps like this:
import { MyOwnDTO } from "libs/my-library";
I'm developing an application using dependency injection with tsyringe. That's the example of a service that receives the repository as a dependency:
import { injectable, inject } from 'tsyringe'
import IAuthorsRepository from '#domains/authors/interfaces/IAuthorsRepository'
#injectable()
export default class ListAuthorsService {
constructor (
#inject('AuthorsRepository')
private authorsRepository: IAuthorsRepository
) {}
And the dependencies container:
import { container } from 'tsyringe'
import IAuthorsRepository from '#domains/authors/interfaces/IAuthorsRepository'
import AuthorsRepository from '#domains/authors/infra/typeorm/repositories/AuthorsRepository'
container.registerSingleton<IAuthorsRepository>(
'AuthorsRepository',
AuthorsRepository
)
export default container
In the tests, I don't want to use the dependencies registered on the container, but instead, to pass a mock instance via parameter.
let authorsRepository: AuthorsRepositoryMock
let listAuthorsService: ListAuthorsService
describe('List Authors', () => {
beforeEach(() => {
authorsRepository = new AuthorsRepositoryMock()
listAuthorsService = new ListAuthorsService(authorsRepository)
})
But I'm receiving the following error:
tsyringe requires a reflect polyfill. Please add 'import
"reflect-metadata"' to the top of your entry point.
What I thought was - "I may need to import the reflect-metadata package before executing the tests". So I created a jest.setup.ts which imports the reflect-metadata package. But another error occurs:
The instance of the repository is somehow undefined.
I would like to run my tests in peace.
First create in root of your project an jest.setup.ts.
In your jest.config.js, search for this line:
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
uncomment, and add your jest.setup.ts file path.
// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
Now import the reflect-metadata in jest.setup.ts:
import 'reflect-metadata';
And run tests again.
I went through the same problem here and refactoring the test find out that it has to import the dependencies first and then the class of service that will be tested
Is it possible for a module to import "itself" by its own name in typescript?
For instance, lets say there is a module my-module with a bunch of test.
Is there a chance to import it inside the tests using import ... from "my-module" instead of using local path (e.g. import ... from "./my-module")?
As I know it can be done using the require, but it seems that import does not support this.
You can use the TypeScript tsconfig paths option e.g.
{
"paths": {
"module": ["src/path/to/module"]
}
}
More
https://www.typescriptlang.org/docs/handbook/module-resolution.html
I have a CRA project in which I've added SSR.
It's supper dummy, it has this structure:
|- App.js
|- Header
|- Header.jsx
|- Header.scss
The App just contains the Header, which is imported without specifying the extension.
import React, { Component } from 'react';
import './App.css';
import Header from './Header/Header';
class App extends Component {
render() {
return (
<Header />
);
}
}
export default App;
With npm start all runs successfully. If I compile the code and run it with node instead, it breaks with this error Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
There seem to be a problem when resolving imports without an extension. It finds the .scss file instead of the .jsx. Specifying the extension on import or removing conlicting files (i.e. Header.scss) solves the issue.
However, I would like to keep using imports without extension, as I normally do in CRA.
I don't know how to specify this on my settings.
I've just pushed this simple example so you can run it yourself: https://github.com/dnaranjo89/resolve_extensions_ssr
I want to use absolute path resolving rules in my project, but when I import a module like this
import Component from "/home/components/Component.js"
it cannot be resolved by webpack, only this works:
import Component from "/users/username/home/components/component.js"
I tried to specify context in my webpack.config, but it made no sense:
context: __dirname
Since I work on windows how do I change my webpack.config to be able to import modules like in the first snippet? In other words how to change my absolute path root for webpack v.2?
You can use resolve.alias to shorten import paths:
webpack.config.js
...
resolve: {
alias: {
home: '/users/username/home/',
},
},
...
Then you will be able to import a module like this:
import Component from "home/components/Component.js"
More information on the official documentation: https://webpack.js.org/configuration/resolve/