AWS CDK -- Cannot find module '#aws-cdk/aws-ec2' - amazon-web-services

I just started playing around with AWS CDK yesterday and I found something very weird.
First of all, I'm using TypeScript for my CDK app (I used cdk init --language typescript to generate the project files and I tried to import aws-ec2 module so this is what I did:
import cdk = require('#aws-cdk/core');
import ec2 = require('#aws-cdk/aws-ec2');
export class vpcStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
//.... all other codes go here....
However, when importing the aws-ec2 module this way, I got this error when trying to deploy the stack:
тип Unable to compile TypeScript:
lib/cdk-type_script-stack.ts:2:22 - error TS2307: Cannot find module '#aws-cdk/aws-ec2'.
2 import ec2 = require('#aws-cdk/aws-ec2');
~~~~~~~~~~~~~~~~~~
Subprocess exited with error 1
This is very weird because the API docs right here clearly stated that this is how I should import the aws-ec2 module in TypeScript

You need to install the node package before you could import and use it
Execute below on the command line to install npm package for aws-cdk
npm i #aws-cdk/aws-ec2

npm install (for install lib)
npm run build (for compile your code)
After that, you can run:
cdk synth
cdk deploy

You may have a version of npm that is incompatible with the version of #aws-cdk/pipelines as explained here: https://github.com/aws/aws-cdk/issues/13541#issuecomment-801606777

In addition to #juned-ashan 's answer, verify that you are installing the correct module version that corresponds to your cdk version (and other cdk modules installed).
For example:
$ npm install --save #aws-cdk/aws-ec2#1.10.0
Note: not enough points to add this as a comment in Juned's answer.

Related

Facing issues in installing packages in aws cdk

I am new to using AWS CDK, I had imported aws_stepfunctions_tasks from aws_cdk.aws_stepfunctions_tasks link given: https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_stepfunctions_tasks/DynamoPutItem.html
but it is showing an import error. Other than that all the imports I have used are working fine. I even tried installing it through pip using different versions and the version which I want but it is giving me the error attached below. Can someone please help out with this issue. I have written the code in my stack file. This issue had occurred also when I was using s3_deploy. I didn't still find a solution for it.
CDK dependencies have changed between V1 and the recently released CDK V2. Make sure you are not mixing V1 and V2 dependencies. Here is a Python example for both versions from aws-samples:
CDK V2
requirements.txt
aws-cdk-lib>=2.0.0
constructs>=10.0.0
app.py
from constructs import Construct
from aws_cdk import (
App, Stack,
aws_lambda as _lambda,
aws_apigateway as _apigw
)
CDK V1
requirements.txt
aws-cdk.core
aws-cdk.aws_lambda
aws-cdk.aws_apigateway
app.py
from aws_cdk import (
core,
aws_lambda as _lambda,
aws_apigateway as _apigw
)

How to use AWS Amplify with Sapper?

My goal is to use AWS Amplify in a Sapper project.
Creating a Sapper project from scratch (using webpack) then adding AWS Amplify and running it in dev is a success, but run it in production throws a GraphQL error in the console (Uncaught Error: Cannot use e "__Schema" from another module or realm).
Fixing this error thows another one (Uncaught ReferenceError: process is not defined).
A solution is to upgrade GraphQL from 0.13.0 to 14.0.0 unfortunatly GraphQL 0.13.0 is an AWS Amplify API dependency.
Does anyone know what can be done to get AWS Amplify work with Sapper in production ?
The link to the repo containing the source files is located here: https://github.com/ehemmerlin/sapper-aws-amplify
(Apologies for the long post but I want to be explicit)
Detailled steps
1/ Create a Sapper project using webpack (https://sapper.svelte.dev).
npx degit "sveltejs/sapper-template#webpack" my-app
cd my-app
yarn install
2/ Add AWS Amplify (https://serverless-stack.com/chapters/configure-aws-amplify.html) and lodash
yarn add aws-amplify
yarn add lodash
3/ Configure AWS Amplify (https://serverless-stack.com/chapters/configure-aws-amplify.html)
Create src/config/aws.js config file containing (change the values with yours but works as is for the purpose of this post):
export default {
s3: {
REGION: "YOUR_S3_UPLOADS_BUCKET_REGION",
BUCKET: "YOUR_S3_UPLOADS_BUCKET_NAME"
},
apiGateway: {
REGION: "YOUR_API_GATEWAY_REGION",
URL: "YOUR_API_GATEWAY_URL"
},
cognito: {
REGION: "YOUR_COGNITO_REGION",
USER_POOL_ID: "YOUR_COGNITO_USER_POOL_ID",
APP_CLIENT_ID: "YOUR_COGNITO_APP_CLIENT_ID",
IDENTITY_POOL_ID: "YOUR_IDENTITY_POOL_ID"
}
};
Add the following code to the existing code in src/client.js:
import config from './config/aws';
Amplify.configure({
Auth: {
mandatorySignIn: true,
region: config.cognito.REGION,
userPoolId: config.cognito.USER_POOL_ID,
identityPoolId: config.cognito.IDENTITY_POOL_ID,
userPoolWebClientId: config.cognito.APP_CLIENT_ID
},
Storage: {
region: config.s3.REGION,
bucket: config.s3.BUCKET,
identityPoolId: config.cognito.IDENTITY_POOL_ID
},
API: {
endpoints: [
{
name: "notes",
endpoint: config.apiGateway.URL,
region: config.apiGateway.REGION
},
]
}
});
4/ Test it
In dev (yarn run dev): it works
In production (yarn run build; node __sapper__/build): it throws an error.
Uncaught Error: Cannot use e "__Schema" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.
https://yarnpkg.com/en/docs/selective-version-resolutions
Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.
5/ Fix it
Following the given link (https://yarnpkg.com/en/docs/selective-version-resolutions) I added this code to package.json file:
"resolutions": {
"aws-amplify/**/graphql": "^0.13.0"
}
6/ Test it
rm -rf node_modules; yarn install
Throws another error in the console (even in dev mode).
Uncaught ReferenceError: process is not defined
at Module../node_modules/graphql/jsutils/instanceOf.mjs (instanceOf.mjs:3)
at \_\_webpack_require\_\_ (bootstrap:63)
at Module../node_modules/graphql/type/definition.mjs (definition.mjs:1)
at \_\_webpack_require\_\_ (bootstrap:63)
at Module../node_modules/graphql/type/validate.mjs (validate.mjs:1)
at \_\_webpack_require\_\_ (bootstrap:63)
at Module../node_modules/graphql/graphql.mjs (graphql.mjs:1)
at \_\_webpack_require\_\_ (bootstrap:63)
at Module../node_modules/graphql/index.mjs (main.js:52896)
at \_\_webpack_require\_\_ (bootstrap:63)
A fix given by this thread (https://github.com/graphql/graphql-js/issues/1536) is to upgrade GraphQL from 0.13.0 to 14.0.0 unfortunatly GraphQL 0.13.0 is an AWS Amplify API dependency.
When building my project (I'm using npm and webpack), I got this warning,
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults
for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
which seems to be related to the schema error, as these posts indicate that the quickest fix for the error is to set NODE_ENV to production in your environment (mode is set to the NODE_ENV environment variable in the webpack config):
https://github.com/aws-amplify/amplify-js/issues/1445
https://github.com/aws-amplify/amplify-js/issues/3963
How to do that:
How to set NODE_ENV to production/development in OS X
How can I set NODE_ENV=production on Windows?
Or you can mess with the webpack config directly:
https://gist.github.com/jmannau/8039787e29190f751aa971b4a91a8901
Unfortunately some posts in those GitHub issues point out the environment variable change might not work out for a packaged app, specifically on mobile.
These posts suggest that disabling the mangler might be the next best solution:
https://github.com/graphql/graphql-js/issues/1182
https://github.com/rmosolgo/graphiql-rails/issues/58
For anyone just trying to get the basic Sapper and Amplify setup going, to reproduce this error or otherwise, I build up mine with:
npm install -g #aws-amplify/cli
npx degit "sveltejs/sapper-template#webpack" my-app
npm install
npm install aws-amplify
npm install lodash (Amplify with webpack seems to need this)
amplify configure
npm run build
amplify init (dev environment, VS Code, javascript, no framework, src directory, __sapper__\build distribution directory, default AWS profile. This generates aws-exports.js.
In src/client.js:
import Amplify from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);
npm run build

Including node-uuid package in Ionic 2 application

I'm building an Ionic 2 (RC0) application and I'm trying to use node-uuid by following the official documentation.
I've done:
$ npm install --save node-uuid
$ npm install --save #types/node-uuid
node-uuid seems to be using the default export approach, so I'm importing it in my typescript file like this:
import uuid from 'node-uuid';
And using it as follows:
console.log(uuid.v4);
However, my app doesn't come up and I see this error in the logs:
TypeError: des$3 is undefined
What am I missing?
Most resources for Angular 2 recommend using the typings CLI to install the type definitions, but this made no difference for me. I tried:
$ npm install --global typings
$ typings install --save node-uuid
$ ionic info
Your system information:
Cordova CLI: You have been opted out of telemetry. To change this, run: cordova telemetry on.
6.3.1
Ionic Framework Version: 2.0.0-rc.0
Ionic CLI Version: 2.1.0
Ionic App Lib Version: 2.1.0-beta.1
OS: Distributor ID: Ubuntu Description: Ubuntu 16.04.1 LTS
Node Version: v6.6.0
******************************************************
Dependency warning - for the CLI to run correctly,
it is highly recommended to install/upgrade the following:
Please install your Cordova CLI to version >=4.2.0 `npm install -g cordova`
******************************************************
Please note that node-uuid is deprecated. They merged with another project and now it's only called uuid. Everything up to the point of installing the #types library is correct. (note that you will have to redo those steps using just 'uuid' not 'nod-uuid')
However,
console.log(uuid.v4);
does not generate an id. As per the documentation you need to specify in your import which version of uuid you want to use and then call your variable as a method: uuid();
From docs: [Deprecation warning: The use of require('uuid') is deprecated and will not be supported after version 3.x of this module. Instead, use require('uuid/[v1|v3|v4|v5]') as shown in the examples below.]
Here is a code example using uuid/v1:
import { Component } from '#angular/core';
import uuid from 'uuid/v1'; //here change 'v1' with the version you desire to use
#Component({
selector: "page-uuid",
templateUrl: "uuid.html"
})
export class uuidTestPage {
id = uuid();
constructor() {
console.log(this.id); // outputs id. For example: 298da400-1267-11e8-a6e5-3148ee6706e9
}
}
After you serve your app and enter the uuidTestPage you should see the id logged to the console. The format of the id will vary depending on the version you use:
Version 1 (timestamp): my example.
Version 3 (namespace)
Version 4 (random)
etc...
Happy coding!
you can try it: (angular2-uuid)
npm install angular2-uuid --save
......
import { UUID } from 'angular2-uuid';
...
let uuid = UUID.UUID();
it works on angular 2 & ionic 2

cannot find package on travis-ci -golang

I'm trying to run a go script as part of the build process. The script imports a 'custom' package. However I get this import error.
The repository name is bis. The script which I run is configbis.go. The package imported configbis.go is mymodule
The project structure is as following:
bisrepo -------
| |
mymodule configbis.go
go run configbis.go
configbis.go:16:2: cannot find package "bisrepo/mymodule" in any of:
/home/travis/.gvm/gos/go1.1.2/src/pkg/bisrepo/mymodule (from $GOROOT)
/home/travis/.gvm/pkgsets/go1.1.2/global/src/bisrepo/mymodule (from $GOPATH)
I've tried to import mymodule in configbis.go as following:
import "mymodule"
import "bisrepo/mymodule"
import "github.com/user/bisrepo/mymodule"
None of them works. I run out of ideas/options ...
I read the the travis-ci documentation and I found it useless.
You could try to add something like that in your .travis.yml:
install:
- go get github.com/user/bisrepo/mymodule
in order to use private repos you must provide a github api auth token (similarly so when deploying go projects which reference private repos on Heroku). You can try adding something like this in your .travis.yml
before_install:
- echo "machine github.com login $GITHUB_AUTH_TOKEN" > ~/.netrc

Cannot find module "laravel-echo"

I am learning to configure Laravel Echo Socket.js and following an article here
Code in bootstrap.js is below
import Echo from "laravel-echo"
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001'
});
When I run the above code, it says, Cannot find module "laravel-echo"
I am not using pusher and redis. In the above reference page, I think, there is no installation guidelines given for only socket.io
Can somebody explain if I am missing anything?
I solved the same problem installing the following packages:
npm install --save laravel-echo pusher-js
I needed to install below packages.
npm install -g laravel-echo-server
then following the step by step instruction as give here
Finally put below code before any js file.
<script src="http://{{ Request::getHost() }}:6001/socket.io/socket.io.js"></script>
the issue is here:-
import Echo from "laravel-echo"
you will write it as:-
import Echo from 'laravel-echo';