I just install all the requirements and setup my new Mac with Apple Silicon M1 Chip for Nativescript 8 development following their guide on https://nativescript.org. Now I created a new project running the command:
ns create
This asks for a name and what flavor you want, I choose Angular. Now I want to start creating new components but when trying to run the following command:
ng g c my-component
I get the following error:
The generate command requires to be run in an Angular project, but a project definition could not be found.
I already have the angular cli installed (version 12 latest) globally. What command should I run to create components or other stuff like services or modules; or do I need another package to do so.
You're missing angular.json in your project. And did you already install NativeScript Schematics? (npm i --save-dev #nativescript/schematics)
The file should have the following content:
{
"version": 1,
"cli": {
"defaultCollection": "#nativescript/schematics"
},
"projects": {
"my-project-name": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "ns"
}
},
"defaultProject": "my-project-name"
}
Replace my-project-name with the name of your project and run the generate command again.
NativeScript Documentation
I use Add Angular Native Files with Visual Studio code. Pretty much works like ng g c componentName. You will just need to add the component to app.module.ts
More info here:
https://marketplace.visualstudio.com/items?itemName=joshdsommer.vscode-add-angular-native-files
Related
I have set up an example project to demonstrate the issue:
https://github.com/garethrbrown/node-share-code
In this example there are two projects, example-api (a mini express project) and example-shared (a class library), both using Node JS / TypeScript. I want example-api to be able to use classes from example-shared to avoid code duplication. Having followed this example, I have referenced the example-shared project from package.json in example-api.
"dependencies": {
"example-shared": "file:..\\example-shared",
"express": "^4.17.1"
}
Having done this, and following running npm install, intellisense in VSCode sees ApiClass from the example-shared project and assists with the import.
I can then run by build command tsc --build via NPM, which succeeds.
I can also see that the sym link has been created in the example-api node_modules directory.
However, when I try to run the example-api project using the npm start script from under example-api, I get an error along the lines of:
Error: Cannot find module 'example-shared/apiClass'
Require stack:
...
code: 'MODULE_NOT_FOUND',
requireStack: [
...
]
I have tried running commands from different locations such as described here, but with no luck so far.
I'm using current stable versions of Node (14+) and NPM (7+).
I don't want to share via NPM or git repositories as I feel it will slow down development.
What am I doing wrong?
Seems like this is a discussed problem, see this post Typescript: How to resolve absolute modules paths for node.js?
I did not investigate further, but in the example-shared folder you can remove "outDir" from your example-shared/tsconfig.json and then run npm run build.
Unfortunately, this will emit the javascript files next to typescript files instead of placing them in a separate directory.
Finally, in the example-api run npm i, npm run build and npm start.
Now, Express will run because Node is using the javascript file instead of typescript file.
As per #mtbno's answer, the standard behavior without "outDir" is all js and map files will just be created next to each of your TypeScript files, which is super gross.
Without using some extra npm package or webpack or any of that, you can solve this by adding that "outDir" in your tsconfig, and then making a couple of tweaks to your package.json.
For argument's sake, say your root TypeScript file is called app.ts and your outDir folder is lib.
Relevant sections in your package.json can look like this:
"scripts": {
"build": "tsc --build",
"clean": "tsc --build --clean",
"start": "npm run build && node ./lib/app"
},
and
"main": "./lib/app",
And here's an example tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"lib": ["es6"],
"sourceMap": true,
"outDir": "./lib",
},
"exclude": [
"node_modules"
]
}
After those 2 files are updated, then running npm run start should build out your lib folder with the compiled JavaScript, and your server should start up successfully. Note that if you previously did a build and you have "old" .js and .map files next to your TypeScript files, you may have to delete those manually because npm run clean won't do it for you.
I am developing two npm packages, say #ffx/alpha and #ffx/beta. Beta package is dependant on Alpha package.
alpha/package.json
{
"name": "#ffx/alpha",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {
"some-package": "1.0.0"
}
}
beta/package.json
{
"name": "#ffx/alpha",
"version": "1.0.0",
"peerDependencies": {
"#ffx/alpha": "^1.0.0"
},
"devDependencies": {
"some-package": "1.0.0"
}
}
For local testing, I am building the Alpha project and install built code using following command inside the Beta project root,
npm install ../alpha/dist/#ffx/alpha
Also, I start building Alpha project in watch mode so that code changes in Alpha project reflect in Beta project (inside node_modules) immediately.
My issue is that, in some scenarios where I use the same dependancy in both Alpha and Beta project (some-package in above setup) Beta project get wrong reference of the same package and throw an error.
Example:
Say Alpha project has following code,
...
let smpkg = new SomePackage();
...
In Beta project, it will throw an error saying types missmatch.
error TS2322: Type 'import("~/alpha/node_modules/some-package").SomePackage' is not assignable to type 'import("~/beta/node_modules/some-package").SomePackage'.
Is there a way to align both references using this method?
Or is there a alternative way to use packages locally before publishing?
NOTE: I've already tried npm link and npm pack with mixed results.
I ran into a similar issue. This is how I managed to fix (I'll use your alpha/beta terminology):
Delete node_modules folders from both alpha and beta
Ensure any matching libraries in the package.json files for both alpha and beta match exactly. (For example, some-package should use 1.x.x instead ^1.x.x)
Run npm install on alpha
Ensure package.json for beta is pointed to local module (For example, #ffx/alpha: file:../alpha/dist/#ffx/alpha)
Run npm install on beta
This ended fixing everything for me.
When I run my Node app via VS Code, it uses the node installed in my Program Files directory, but I specifically want to run it with a different version of Node, which is installed in my C:\NodeJS folder.
Is this possible?
This might also be helpful:
"nvm" support
If you are using "nvm" (or "nvm-windows") to manage your
Node.js versions it is now possible to specify a runtimeVersion
attribute in a launch configuration for selecting a specific version
of Node.js. Here is an example launch config:
{
"type": "node",
"request": "launch",
"name": "Launch test",
"runtimeVersion": "7.10.1",
"program": "${workspaceFolder}/test.js"
}
Please note: Make sure to have those Node.js versions installed that
you want to use with the runtimeVersion attribute as the feature will
not download and install the version itself. So you will have to run
something like nvm install 7.10.1 from the integrated terminal if you
plan to add "runtimeVersion": "7.10.1" to your launch configuration.
From node debugging. So you can set a node version in a launch config for purposes of debugging, but their doesn't appear to be a similar option for a task in tasks.json.
I want to integrate some kind of code linting for node.js in webstorm so I installed standard to my node.js project using:
npm instal standard --save-dev
It was installed and listed in the "devDependencies" section of package.json but when I run the command:
standard
in the console I get
'standard' is not recognized as an internal or external command
if you want to use it locally you have to include it in you scripts first in package.json
"scripts": {
"standard": "standard",
"standard::fix": "standard --fix"
}
and use npm run standard to run it. or if you are using yarn type yarn standard
The scripts are in node_modules\.bin.
So, either:
Add this to PATH before running standard, e.g.:
set PATH=%PATH%;node_modules\.bin
Run it in using node_modules\.bin\standard
Use #tarek's approach using package.json: https://stackoverflow.com/a/49026837/122441
"scripts": {
"test": "standard middlewares/validations.js"
}
Add above lines in package.json.
Here middlewares/validations.js is the path of the file to check.
Run -> npm test
If this file have any error you will get.
I have installed below software in my System
VS 2017- Version 15.3.3
Node Version - 6.11.3
Angular cli Version - 1.4.1
I want to create component using angular cli inside angular template in VS 2017
Below are the step I am doing but fail to achive the results
Step 1) Create a Project with Angular Template in VS2017
Step 2)Open Cmd - Create a dummy ng project for .angular-cli.json file outside the solution
Step 3)Copy Pasting .angular-cli.json file to my solution under below folder structure
[M:\VS2017 Projects\Trail109\Trail109]
Step 4)Changing from "root": "Src" to "root": "ClientApp"
Step 5)then going inside the solution using cd command in cmd
Step 6)and installing angular/cli for my solution using given command npm install #angular/cli#latest --save-dev
[M:\VS2017 Projects\Trail109\Trail109>npm install #angular/cli#latest --save-dev]
Step 7)after this going to components folders inside ClientApp using cd command in cmd
Step 8)and using the given command ng g component myformname
[M:\VS2017 Projects\Trail109\Trail109\ClientApp\app\components>ng g component myformname]
but the above line gives me below given error
Error: Path "ClientApp/" is invalid.
Path "ClientApp/" is invalid.
Please help What is that I am missing....
VS 2017 angular template has pre rendering enabled by default. If you haven't disabled that, please make sure after changing root to Client app
Amend the .angular.cli : change as follows
"root": "ClientApp",
"outDir": "ClientApp/dist",
Amend the ClientApp folder as follows
Rename app.module.client.ts to app.client.module.ts
Open app.client.module.ts: prepend the declaration with 3 dots “...” and wrap the declaration in brackets.
For example: [...sharedConfig.declarations, ]
Open boot-client.ts: update your import to use the new app.client.module reference.
For example: import { AppModule } from './app/app.client.module';
Open your project file in notepad.
find the tag name which stated with SpaRoot
And Replace your angular project folder name here.