When I look this page they use npm install --save lodash to install lodash. But when I look this page, the package.json looks like this:
{
"name": "webpack-demo",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
===> "devDependencies": { <===
"webpack": "^4.0.1",
"webpack-cli": "^2.0.9",
"lodash": "^4.17.5" <- ???
}
}
Am I miss something?
No need to get confused. It's pretty simple.
use below command if you need to lodash in the production
npm install lodash --save
use below command if you don't need to lodash in the production
npm install lodash --save-dev
Once you are done. lodash's functions will be available globally. If you get any error like _ is not defined. Then you can import lodash like this
import _ from 'lodash'; OR let _ = require('lodash')
Related
i'm new at babel and browserify, I have a problem with transpile with browserify and babel, i've installed the threejs package and add
import * as THREE from 'three'
it works fine when i transpiled using command below
browserify input.js > output.js -t babelify
but when i add another import
import {GLTFLoader} from '../node_modules/three/examples/jsm/loaders/GLTFLoader'
and i used that command again, it doesn't work and it says
'import' and 'export' may appear only with 'sourceType: module' (1:0)
i've also added type module in the package.json but it still doesn't work, here is my package.json
{
"name": "three.js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"three": "^0.129.0"
},
"devDependencies": {
"#babel/core": "^7.14.5",
"#babel/preset-env": "^7.14.5",
"babelify": "^10.0.0"
},
"type": "module"
}
also here is my babel.config.json
{
"presets": ["#babel/preset-env"]
}
is there anything i need to add or change?
Importing the examples from the js folder would work
import {GLTFLoader} from './node_modules/three/examples/js/loaders/GLTFLoader'
// or depending on your path
import {GLTFLoader} from '../node_modules/three/examples/js/loaders/GLTFLoader'
The files in the jsm folder imports from three.module.js
I have created git repository which will be used as npm package in other project. Lets say that sharable repository name is genesis-service-broker.
I am using this shareable repository inside one of the service(activation service). In this project, I am installing package using yarn. Its running perfectly fine here.
"dependencies": {
...
"genesis-service-broker": "git+https://${key}:x-oauth-basic#git.my_project.com/${user}/genesis-service-broker.git#create_service_broker_for_genesis",
...
}
When I am trying to use genesis-service-broker package inside onother service(partner service) its not able to install the dependencies. In this project, I am installing dependencies using npm. If I install dependencies using yarn its working perfectly fine.
I am not getting any errors in npm install command. I am just not able to find genesis-service-broker folder inside node_modules, when I am installing dependencies using npm.
package.json file inside genesis-service-broker repository. (for reference purposes)
{
"name": "service-broker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git#git.my_project.com:amol.barewar/service-broker.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"config": "^3.2.5",
"form-data": "^3.0.0",
"node-fetch": "^2.6.0",
"request": "^2.88.0",
"uuid": "^3.4.0"
}
}
there is a difference in behaviour here, between yarn and npm
yarn add retains the name of the git project in dependencies, and creates a folder with the same name in node_modules.
So, yarn add git+https://${key}:x-oauth-basic#git.my_project.com/${user}/genesis-service-broker.git#create_service_broker_for_genesis will lead to a module getting installed as node_modules/genesis-service-broker
On the other side, npm install gets the name from the name property in package.json; and it will lead to module getting added as node_modules/service-broker in your case... and also the dependencies map will be like
"dependencies": {
...
"service-broker": "git+https://${key}:x-oauth-basic#git.my_project.com/${user}/genesis-service-broker.git#create_service_broker_for_genesis",
...
}
Because of this difference, the requires(...) might fail.
As, in this case, with yarn that module will be available through -
require('genesis-service-broker')
And for npm through -
require('service-broker')
So, all in all, it will help to keep the name property in package.json same as the project name.
I'm trying to run a Mocha test with some ES6 style imports in the file but I keep getting the error:
import assert from 'assert';
^^^^^^
SyntaxError: Unexpected identifier
I tried to invoke mocha with both
mocha --require #babel/register --recursive
and
mocha --require babel-register --recursive
but the error would not go away.
What is the correct way to run ES6 style Mocha tests?
For anyone coming from Google:
You can also install esm: npm i esm --save-dev or use your preferred package manager.
Then pass it as an argument to mocha: mocha 'index.test.js' --require esm
Try Below Code
import { strict as assert } from 'assert';
Or
import * as assert from 'assert';
Hope this helps
I found the answer to my question here -> https://dev.to/bnorbertjs/my-nodejs-setup-mocha--chai-babel7-es6-43ei
This package.json file
{
"name": "mochatest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha --require #babel/register --recursive"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.1.2",
"#babel/preset-env": "^7.5.4",
"#babel/register": "^7.4.4",
"mocha": "^6.1.4"
},
"dependencies": {}
}
together with this .babelrc
{
"presets": ["#babel/preset-env"]
}
solved my problem.
I'm creating a npm package, that requires axios.
When i import it in my react application i get
Module not found: Can't resolve 'axios' in
'/Users/******/nodework/consume/node_modules/myModule'
I want the client to be able to install my package module, without having to install another dependency on their end. How would i do that give that im already using peerDepencies and devDependencies ?
for example this is my package.json for my module.
{
"name": "myModule",
"version": "1.6.4",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"axios": "^0.19.0"
},
"peerDependencies": {
"axios": "^0.19.0"
}
}
Peer dependencies lets developers who are using your library know they need to install those libraries themselves because the library you wrote is using it. They will get "missing peer dependency" warnings when they install your library and do not have those installed yet.
The dev dependencies are typically for build tools like webpack, compilers, etc.
Both peer and dev dependencies will not be installed when a developer installs your library.
The clients will have to do something like npm i your-library axios to install both your library and the peer dependency.
If you want the developers to solely install your library without having to install extras on their own, like axios in this case, you will have to list is as a regular dependency.
{
"dependencies": {
"axios": "^0.19.0"
}
}
Cheers
I'm setting up a dockerized dev environment for node/typescript for an api project. The goal is to run everything in docker and not have any of installed node, npm or modules installed on the host. This is to isolate all versions of node and all modules from other projects.
./node
docker run \
-it \
-p "8080:80" \
--rm \
-w "/app" \
-v "$(pwd):/app" \
"node:10" "$#"
./npm
#!/bin/sh
./node npm $#
./npx
#!/bin/sh
./node npx $#
./package.json
{
"name": "testapi",
"version": "0.0.1",
"description": "a hello world api",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "npx ts-node src/app.ts",
"lint": "npx ts-lint --project src $#"
},
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"fastify": "^1.13.2",
"ts-node": "^7.0.1"
},
"devDependencies": {
"#types/node": "^10.12.15",
"ts-lint": "^4.5.1",
"typescript": "^3.2.2"
}
}
[edit]
I use ./npm install to build my node_modules. The node_modules is in a shared volume so it persists on the host after the container is removed. That way I don't need a Dockerfile to build an image.
[/edit]
When I run the lint command, I get the following error:
testapi$ ./npx ts-lint -i
10: Pulling from node
Digest: sha256:5af431757f84bf7878ff72447eb993fc37afcd975874fff13278157bf83661e6
Status: Image is up to date for docker-remote.registry.kroger.com/node:10
npx: installed 32 in 2.883s
Cannot find module 'typescript'
I think this has to do with module resolution, but I don't know this for sure. I see people install typescript globally, but that would mean I have to do a Dockerfile instead of using the stock node image. I don't mind using a Dockerfile for dev, but I think there should be a way to make this work without doing that.
So I figured out the answer. It wasn't obvious, and I stumbled upon it by accident.
I had installed ts-lint (see package.json above), and I saw an example which referenced tslint (without the hyphen).
So I removed ts-lint and installed tslint and it worked like a champ. I'm not sure what the difference is, but the one with the hyphen does not work in my project configuration. Also, the one without the hyphen installed a higher version number than the one with the hyphen.
See my new package.json containing the working dependency:
{
"name": "testapi",
"version": "0.0.1",
"description": "a hello world api",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "npx ts-node src/app.ts",
"lint": "npx tslint --project ./ 'src/**/*.ts?(x)' $#"
},
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"fastify": "^1.13.2",
"ts-node": "^7.0.1"
},
"devDependencies": {
"#types/node": "^10.12.15",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
}
}
This works when run in a docker container, just using the public node:10 image. It doesn't need a Dockerfile to install any global dependencies.
Try
yarn global add tslint typescript
or if it complained for permissions:
sudo yarn global add tslint typescript