i am trying to fetch data from the db.json.
here is my db.json file:
{
"posts": [
{ "id": "react-hooks", "title": "React Hooks", "content": "The greatest thing since sliced bread!", "author": "ali" },
{ "id": "react-fragments", "title": "Using React Fragments", "content": "Keeping the DOM tree clean!", "author": "ali" }
],
"users": [
{ "id": 1, "username": "ali", "password": "********" }
],
"themes": [
{ "id": 1, "primaryColor": "deepskyblue", "secondaryColor": "coral" },
{ "id": 2, "primaryColor": "orchid", "secondaryColor": "mediumseagreen" },
{ "id": 3, "primaryColor": "darkslategray", "secondaryColor": "slategray" }
]
}
after that i have executed > npm install --save json-server and > npx json-server --watch server/db.json and i have edited the "scripts": part as followed:
"start:server": "npx json-server --watch server/db.json",
"start:client": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
then i have excuted > npm install --save concurrently and then i have added that line in the "scripts":
"start": "npx concurrently \"npm run start:server\" \"npm run start:client\"",
then i have executed > npm install --save http-proxy-middleware and after that i have created setupProxy.js and put the following code:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports=function(app){
app.use(createProxyMiddleware('/api',{
target:'http://localhost:5000',
pathRewrite:{'^/api':''}
}))
}
then i excuted npm install and after that when i execute npm start it shows the following error:
The system cannot find the file :client.
[5] start:client exited with code 1
[2] The system cannot find the file :server.
[2] start:server exited with code 1
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! reacttutorial#0.1.0 start: `npx concurrently "npm run start:server" "npm run start:client"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the reacttutorial#0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
and there are two windows alerts which are saying that ':server' and ':client' cannot be found.
i have created whole new app by executing npx create-react-app reacttutorial. i am just running out of ideas. can anyone help me on that please?
look at this reply React scripts fails while i run concurrently with nodemon
so you put in
"scripts": {
"dev": "concurrently \"npm run server\" \"npm run react\"",
"server": "npx json-server --watch server/db.json --port 4000 --routes server/routes.json",
"react": "react-scripts start",
...}
and use npm run dev instead or npm start
worked for me
and also use createProxyMiddleware instead of proxy in setupProxy.js file
Related
I'm using WSL Ubuntu to run a react-relay project, but after installing watchman using brew and trying to run the app in watch mode, watchman gives me the following error(I have run the project using yarn start):
Watchman error: The watchman server reported an error: "watchman::QueryExecError: query failed: synchronization failed: syncToNow: timed out waiting for cookie file to be observed by watcher within 60000 milliseconds: Connection timed out", while executing command: QueryRequest(
"query",
"/mnt/d/Work/Personal/react-relay-tutorial",
QueryRequestCommon {
glob: None,
glob_noescape: false,
glob_includedotfiles: false,
path: Some(
...
I've been trying reinstalling watchman without brew, with brew, the same error I get, nothing on the internet helped...
My package.json file:
...
"scripts": {
"start": "yarn run relay && react-scripts start",
"build": "yarn run relay && react-scripts build",
"relay": "yarn run relay-compiler --watch $#",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"relay": {
"src": "./src/",
"schema": "./src/schema.graphql"
},
...
"devDependencies": {
"babel-plugin-relay": "^13.1.1",
"graphql": "^16.3.0",
"relay-compiler": "^13.1.1"
}
...
relay.config.js file
module.exports = {
// ...
// Configuration options accepted by the `relay-compiler` command-line tool and `babel-plugin-relay`.
src: './src',
schema: './src/schema.graphql',
exclude: ['**/node_modules/**', '**/__mocks__/**', '**/__generated__/**'],
};
.babel.rc
{
"plugins": [
"relay"
]
}
Project structure:
PS: when running the project without any watchman it works just fine
I have a series of Jenkinsfiles that looks like this (this is the production version):
pipeline {
agent {
dockerfile {
filename 'Dockerfile'
dir 'jenkins/'
label 'agent && linux'
args '''
-v /var/lib/package-cache/.npm:/.npm:rw,z
'''
}
}
environment {
NODE_ENV = "production"
}
stages {
stage('Install dependencies') {
steps {
sh "npm ci --production=false"
sh "npx cypress install"
sh "npm install -g cross-env"
}
}
stage('Cypress tests') {
parallel {
stage('Start local server') {
steps {
sh returnStatus: true, script: "npm run start:test"
}
}
stage('Run Cypress tests') {
steps {
sh 'npm run wait'
sh 'cross-env NODE_ENV=test npm run cypress:run'
sh 'killall node'
}
}
}
}
stage('Build') {
steps {
sh "npm run build:${NODE_ENV}"
}
}
stage('Deploy') {
steps {
sh "npm run publish:${NODE_ENV}"
}
}
}
post {
always {
echo "Job completed"
}
}
}
Note that the versions for the Jenkinsfile for the other environments use that particular environment (development, qa, and staging) for the NODE_ENV = "{ENV}" line.
The scripts in my package.json includes the following:
{
"start": "cross-env TS_NODE_PROJECT=\"tsconfig-for-webpack-config.json\" webpack-dev-server --http --port 9003",
"start:development": "cross-env NODE_ENV=development npm run start",
"start:test": "cross-env NODE_ENV=test npm run start",
"start:qa": "cross-env NODE_ENV=qa npm run start",
"start:staging": "cross-env NODE_ENV=staging npm run start",
"start:production": "cross-env NODE_ENV=production npm run start",
"cypress:run": "cypress run",
},
In all non-production environments, the npm run start:test line in the Jenkinsfile correctly uses test for NODE_ENV when webpack dev server starts. When the production Jenkinsfile is used, however, it ends up using production for NODE_ENV rather than test when npm run start:test is called.
What would be causing my npm run start:test script to be ignoring the value (of test) I'm setting via cross-env and, instead, using production?
I'm having a problem making a unitary test with jest over an es6 class. I don't know if the configurations were made properly or if I need some extra package.
This is my file queue.js
export default class Queue {
constructor() {
...
}
//Adds a new elemnt to the queue
method1(element) {
...
}
.
.
.
}
This is my test file
import Queue from "../src/queue";
const myQueue = new Queue();
describe("Queue", () => {
...
...
...
});
This is my .babelrc file
{
"presets": ["es2015"]
}
This is my package.json file. The clean, build and production scripts run all ok, but when I try to run the test, then an error is thrown.
{
"name": "queue",
"version": "1.0.0",
"description": "An implementation of a queue data structure",
"main": "queue.js",
"scripts": {
"test": "jest",
"clean": "rm -dr dist",
"build": "npm run clean && mkdir dist && babel src -s -d dist",
"production": "npm run build && node bin/production"
},
"author": "Osiris Román",
"license": "ISC",
"jest": {
"transform": {
"^.+\\.js$": "babel-jest"
}
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-jest": "^25.4.0",
"babel-preset-es2015": "^6.24.1",
"babel-register": "^6.26.0",
"jest": "^25.3.0"
}
}
This is the error:
Plugin/Preset files are not allowed to export objects, only functions. In /home/usuario/practicing/javascript/queue/node_modules/babel-preset-es2015/lib/index.js
Does someone know how can I solve this problem?
The error you are getting means that one of your presets isn't compatible with the babel version.
Looking at your package.json you are using babel version 6. But both jest and babel-jest use later versions of babel. This causes your es2015 preset not to work correctly.
If you are tied to your current version of babel you can downgrade your jest and babel-jest dependencies to a version that uses older versions of babel:
npm uninstall --save-dev babel-jest jest
npm install --save-dev babel-jest#23.6.0 jest#23.6.0
If not, I would recommend to upgrade your babel version (babel-cli, babel-register and babel-preset-es2015 packages) to newer versions.
If you follow this second path, note that babel-preset-es2015 is deprecated and the use of #babel/preset-env is recommended instead.
I finally found how to solve the problem. I'll share here the configuration If someone else is having the same problem.
This is my file queue.js
export default class Queue {
constructor() {
...
}
//Adds a new elemnt to the queue
method1(element) {
...
}
.
.
.
}
This is my test file
import Queue from "../src/queue";
const myQueue = new Queue();
describe("Queue", () => {
...
...
...
});
I decided to use babel.config.js file insted of .babelrc file. This is the content of this new file.
module.exports = {
presets: [["#babel/preset-env", { targets: { node: "current" } }]],
};
This is my package.json file. The clean, build, dev, production and test scripts run all ok without errors.
{
"name": "queue",
"version": "1.0.0",
"description": "An implementation of a queue data structure",
"main": "queue.js",
"scripts": {
"test": "jest",
"clean": "rm -dr dist",
"build": "npm run clean && babel src -s -d dist ",
"production": "npm run build && node bin/production",
"dev": "npm run build && node bin/dev"
},
"author": "Osiris Román",
"license": "ISC",
"devDependencies": {
"#babel/cli": "^7.8.4",
"#babel/core": "^7.9.0",
"#babel/preset-env": "^7.9.5",
"#babel/register": "^7.9.0",
"babel-jest": "^25.5.0",
"jest": "^25.3.0"
}
}
Thanks to #mgarcia comment I decided to avoid babel-preset-es2015 to use the recommended #babel/preset-env .
I am working on a tool and I have installed some modules locally through NPM, and I get errors when I try to require these modules through NodeJS. I am working in Windows 10, I have already tried setting up NODE_PATH etc.
Below is the structure of my files:
->project
---->node_modules
---->src
-------->css
-------->js
----------->index.js
---->package.json
---->index.html
I populate the index.html by using index.js etc.
Below is the code of my package.json:
{
"name": "bip39",
"version": "1.0.0",
"description": "A tool for converting BIP39 mnemonic phrases to addresses and private keys.",
"directories": {
"test": "tests"
},
"dependencies": {
"bip39": "^2.6.0",
"bigi": "^1.4.2",
"create-hmac": "^1.1.7",
"nem-sdk": "^1.6.7"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/PavlosTze/bip39.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/PavlosTze/bip39/issues"
},
"homepage": "https://github.com/PavlosTze/bip39#readme"
}
Below is my require() code:
var createHmac = require("create-hmac");
var BigInteger = require("bigi");
const bip39 = require("bip39");
const nem = require("nem-sdk").default;
I have done the following steps on NPM:
1) npm init
2) npm install bip39
3) npm install nem-sdk
4) npm install bigi
5) npm install create-hmac
All these files are inside the node_modules, but still, whenever I run the code in the browser I get an 'Error: Cannot find module "bip39"', etc. for all modules.
EDIT: On another directory that I have cloned the same tool, before I merged it with another one, I get no error and everything works correctly, including the require(). etc. Do you need any of those files as well?
Can someone help me please?
try this :
rm -rf node_modules
npm install
So when I run locally the mocha test inside my repo It works just fine but when I push a commit and it runs on travis CI I keep getting this error:
sh: 1: mocha: Permission denied
npm ERR! Test failed. See above for more details.
my .travis.yml
language: node_js
node_js:
- "6"
install:
-npm install
before_script: npm install -g mocha
before_script: chmod 0777 ./node_modules/.bin/mocha
package.json
{
"name": "skeletonapp",
"version": "0.0.0",
"description": "skeletonapp",
"main": "index.js",
"author": {
"name": "li"
},
"scripts": {
"test": "mocha"
},
"dependencies": {
"chai": "^3.5.0",
"mocha": "^3.1.2"
}
}
structure of files
https://travis-ci.org/LiamDotPro/Travis-CI-NodeJS-Skeleton---Mocha-Chai/builds/173340318
test.js
var assert = require('assert');
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
assert.equal(-1, [1, 2, 3].indexOf(4));
});
});
});
Turn on the execute permission on node_modules/.bin: https://github.com/travis-ci/travis-ci/issues/6828#issuecomment-258581083
Or, remove node_modules from your Git repository, and let Travis install it with npm install. (See https://docs.travis-ci.com/user/languages/javascript-with-nodejs#Travis-CI-uses-npm.)
Do not install mocha to Travis-CI manually. It's here by default.
Just remove your before_script's statements from the .travis.yml file.