Skip to content

Node Package Manager (NPM)#

What Is The Node Package Manager?#

  • Node Package Manager (NPM) is basically a command line tool as well as a registry of third party libraries that we can add to our Node applications.
  • It allows developers to easily manage and install external libraries, modules, and tools (called packages or modules) that extend the functionality of Node.js applications. NPM is the default package manager for Node.js and is included with the Node.js installation.
  • Key features and functionalities of NPM include:
Feature Description
Package Installation NPM allows developers to install packages from the official npm registry or from private registries. The installation process automatically handles the resolution of dependencies, ensuring that all required modules are downloaded and accessible.
Version Management NPM provides version management for packages. Developers can specify the desired version of a package in the package.json file or use version ranges to allow updates within specific constraints.
Package Publishing Developers can publish their own packages to the npm registry, making them available to others to use and contribute to. This fosters collaboration and code sharing within the Node.js community.
Dependency Management NPM tracks dependencies and devDependencies of a project in the package.json file. This enables automated installation and management of all required modules by running a single command (npm install).
Script Execution NPM allows developers to define custom scripts in the package.json file, which can be executed using the npm run command. This feature is commonly used for automation, running tests, and other development tasks.
Scopes NPM introduced scopes, allowing developers to organize packages under specific namespaces to avoid naming conflicts.

Package.json#

  • package.json is basically a json file that includes some basic information about our application or our project, such as it's name, it's version, it's authors, the address of its git repository, its dependencies and so on. It's basically a bunch of metadata about our application. And all Node applications by standard have this package.json file.
  • To use NPM in a Node.js project, we need to initialize a package.json file using the npm init command, which will prompt us for some basic information about our project and its dependencies.
1
npm init
  • After fill some basic information, we would have a package.json file that contains some basic information about our node application as below.
package.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
  "name": "npm-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Installing A Node Package#

  • After that, we can use npm install <package-name> to install latest packages from npm registry and npm install to install all dependencies specified in the package.json file.
  • For example, in our project npm-demo we will install package underscore to our node application.
1
npm install undersorce
  • After execute the command above in npm-demo project, then we can see in the package.json the dependency underscore added with the version.
package.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
  "name": "npm-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "underscore": "^1.13.6"
  }
}
  • Moreover, you can also see there is folder node_modules inside our npm-demo application which contains the package underscore and inside it we will also see the package.json

 #zoom

  • So it means every npm dependency is a node application like our npm-demo application and it is stored in the a folder called node_modules.
  • Now, we will be confused that where does npm download this dependency from? The answer is that by default after installing the nodejs , the dependencies will be download from the default registry https://registry.npmjs.org/ of NPM. We can use the command below to check which registry that we are using.
1
2
npm get registry
https://registry.npmjs.org/
  • Finally, if we want to find a public dependency or just view the information about the dependency that we are using. We can go to npmjs.com to search and review.

 #zoom

Using A Package#

  • To use a dependency package, we just need to include it like we load a module by using the require keyword.
1
2
3
4
5
const _ = require('underscore');

//Core module
//File or Foler
//node_modules
  • So with the require require. Firstly, it will assume that the module name that we supply is the core module. However, in Nodejs we don't have this module so the require function thinks that maybe it is a file or a folder in this project. However, to reference a file we have to use the ./ in the argument and then the require function will assume that there is a underscore.js file in the same folder or there is an index.js file in the folder underscore (underscore/index.js). Finally, the require function move to the third step and assume that the supply module exists inside the node_modules folder. So this is how the require function worked.

  • Now, let's take an example with underscore dependency. Let's create a file index.js and add the example code as below.

index.js
1
2
3
4
5
6
7
const _ = require('underscore');

var result = _.contains([1, 2, 3], 2);
console.log(result);

//Result
//true

Package Dependencies#

  • Every time we install a package with npm install the dependencies in package.json will be updated.
1
npm install mongoose
package.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
  "name": "npm-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongoose": "^7.4.1",
    "underscore": "^1.13.6"
  }
}
  • Then if we look at the node_modules folder there are many child folder added although we only installed mongoose package. These are other node packages that the mongoose is dependent on.
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo/node_modules$ ll
total 108
drwxrwxr-x 24 duc duc 4096 Thg 7  28 08:33  ./
drwxrwxr-x  3 duc duc 4096 Thg 7  23 14:45  ../
drwxrwxr-x  6 duc duc 4096 Thg 7  28 08:33  bson/
drwxrwxr-x  4 duc duc 4096 Thg 7  28 08:33  debug/
drwxrwxr-x  3 duc duc 4096 Thg 7  28 08:33  ip/
drwxrwxr-x  2 duc duc 4096 Thg 7  28 08:33  kareem/
drwxrwxr-x  2 duc duc 4096 Thg 7  28 08:33  memory-pager/
drwxrwxr-x  5 duc duc 4096 Thg 7  28 08:33  mongodb/
drwxrwxr-x  3 duc duc 4096 Thg 7  28 08:33  mongodb-connection-string-url/
drwxrwxr-x  7 duc duc 4096 Thg 7  28 08:33  mongoose/
drwxrwxr-x  4 duc duc 4096 Thg 7  28 08:33  mpath/
drwxrwxr-x  4 duc duc 4096 Thg 7  28 08:33  mquery/
drwxrwxr-x  2 duc duc 4096 Thg 7  28 08:33  ms/
-rw-rw-r--  1 duc duc 9536 Thg 7  28 08:33  .package-lock.json
drwxrwxr-x  2 duc duc 4096 Thg 7  28 08:33  punycode/
drwxrwxr-x  4 duc duc 4096 Thg 7  28 08:33  saslprep/
drwxrwxr-x  6 duc duc 4096 Thg 7  28 08:33  sift/
drwxrwxr-x  5 duc duc 4096 Thg 7  28 08:33  smart-buffer/
drwxrwxr-x  5 duc duc 4096 Thg 7  28 08:33  socks/
drwxrwxr-x  2 duc duc 4096 Thg 7  28 08:33  sparse-bitfield/
drwxrwxr-x  3 duc duc 4096 Thg 7  28 08:33  tr46/
drwxrwxr-x  5 duc duc 4096 Thg 7  28 08:33 '@types'/
drwxrwxr-x  5 duc duc 4096 Thg 7  23 13:55  underscore/
drwxrwxr-x  3 duc duc 4096 Thg 7  28 08:33  webidl-conversions/
drwxrwxr-x  3 duc duc 4096 Thg 7  28 08:33  whatwg-url/
  • Okay, at this point maybe we will have some questions, so we knew that a dependency package is like a node application but why do dependency packages inside the node_modules do not contain a folder node_modules like our npm-demo application that we are working on?
    • The answer is if every dependency package contains a node_modules then there is a case a dependency will create a very deeply nested structure and on Windows Os specifically there is a limitation on the number of characters that we can have in a path.
    • So the behavior of the dependency package in the node_modules has changed, now all dependencies of our application as well as their dependencies are stored under node_modules
  • Okay, now we have another question, how can the NPM handle multi versions of an dependency?. For example, in the npm-demo we will use the package bson version 5.3.0 but in the dependency mongoose it is using the package bson version 5.4.0.
    • In this case, our bson package of npm-demo application will be stored in the folder node_modules with version 5.3.0. Then in the mongoose dependency a new node_modules folder will generated and store the bson with version 5.4.0. Let's see the example below.
1
npm install bson@5.3.0

 #zoom

  • As you can see, the bson version 5.3.0 that our application npm-demo is stored in the main node_modules.

 #zoom

  • Then the bson version 5.4.0 of mongoose dependency is stored inside the node_modules folder of mongoose package.
  • So This is how the NPM manages the dependencies.

NPM Packages And Source Control#

  • There is a problem with the node_modules folder, if our application use a lot of node dependencies then our node_modules folder will be very heavy with hundred megabytes and when some one has to clone our source code from the repository then he have to wait for a long time for downloading.
  • To handle that problem the NPM will keep the information of dependencies that are used in our application in package.json field dependencies and in the case we delete the node_modules folder, we don't have to run npm install <packageName> for every dependency again, we just need to run npm install then all dependencies will be downloaded into the node_modules folder from public registry again. So now, we can exclude the folder node_modules from our source code.

Semantic Versioning#

  • Okay now, if we look into the dependencies in the package.json we may see the character ^ before the version, so what is it mean?
  • Before answering this question we need to understand the Semantic Versioning. So what is the Semantic Versioning?
    • Semantic Versioning (SemVer) is a versioning system for software packages that provides a consistent and reliable way to communicate changes between versions.
    • SemVer versions are made up of three numbers:
      • Major version: This number indicates a significant change and break existing APIs or functionality of the software.
      • Minor version: This number indicates a minor change but doesn't break the existing API in the software, such as adding new features.
      • Patch version: This number indicates a patch release, such as a bug fix or a security fix.
  • So the ^ tells the NPM that if there is any newer Minor or Patch version so they will be updated in node_modules as long as the Major version is not changed.
package.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
  "name": "npm-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bson": "^5.3.0",
    "mongoose": "^7.4.1",
    "underscore": "^1.13.6"
  }
}
  • For example, if we are using the bson version ^5.3.0 and using ^ then we are talking to NPM that if there is any newer Minor or Patch versions (ex: ^5.4.0) please use it when restore the package in npm install. It means when some one clone our project from the repository and run npm install. Then the bson will be installed with higher Minor or Patch versions for example ^5.4.0.
  • Beside the character ^ we can also use the exact Major version with x. For example: 5.x

  • Next, in some case we usually also see the character ~, so it means if there is any newer Patch version so they will be updated in node_modules as long as the Major and Minor versions are not changed and we also have an alternative syntax with Major, Minor and x . For example, 5.4.x.

  • Finally, if we want to use the exact version for dependencies we just simply put the exact version without using any ^ or ~ or version with x.

Listing The Installed Packages#

  • Now, if we want to know what are actual dependencies versions that we are using in our application we can simply use the command below
1
npm list
1
2
3
4
npm-demo@1.0.0 /home/duc/study/nodejs-backend/udemy/npm-demo
├── bson@5.4.0
├── mongoose@7.4.1
└── underscore@1.13.6
  • Then if we need to check all dependencies versions in the node_modules we can use the command.
1
npm list --all
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
npm-demo@1.0.0 /home/duc/study/nodejs-backend/udemy/npm-demo
├── bson@5.4.0
├─┬ mongoose@7.4.1
│ ├── bson@5.4.0 deduped
│ ├── kareem@2.5.1
│ ├─┬ mongodb@5.7.0
│  ├── UNMET OPTIONAL DEPENDENCY @aws-sdk/credential-providers@^3.201.0
│  ├── UNMET OPTIONAL DEPENDENCY @mongodb-js/zstd@^1.1.0
│  ├── bson@5.4.0 deduped
│  ├── UNMET OPTIONAL DEPENDENCY kerberos@^2.0.1
│  ├── UNMET OPTIONAL DEPENDENCY mongodb-client-encryption@>=2.3.0 <3  ├─┬ mongodb-connection-string-url@2.6.0
│   ├─┬ @types/whatwg-url@8.2.2
│    ├── @types/node@20.4.5
│    └── @types/webidl-conversions@7.0.0
│   └─┬ whatwg-url@11.0.0
│     ├─┬ tr46@3.0.0
│      └── punycode@2.3.0
│     └── webidl-conversions@7.0.0
│  ├─┬ saslprep@1.0.3
│   └─┬ sparse-bitfield@3.0.3
│     └── memory-pager@1.5.0
│  ├── UNMET OPTIONAL DEPENDENCY snappy@^7.2.2
│  └─┬ socks@2.7.1
│    ├── ip@2.0.0
│    └── smart-buffer@4.2.0
│ ├── mpath@0.9.0
│ ├─┬ mquery@5.0.0
│  └─┬ debug@4.3.4
│    └── ms@2.1.2
│ ├── ms@2.1.3
│ └── sift@16.0.1
└── underscore@1.13.6

Viewing Registry Info For A Package#

  • Okay as we knew before, we can access the link npmjs.com to view details about the package that we are using. However, there is another way by using the command below.
1
npm view <packageName>
1
npm view mongoose
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm view mongoose

mongoose@7.4.1 | MIT | deps: 7 | versions: 803
Mongoose MongoDB ODM
https://mongoosejs.com

keywords: mongodb, document, model, schema, database, odm, data, datastore, query, nosql, orm, db

dist
.tarball: https://registry.npmjs.org/mongoose/-/mongoose-7.4.1.tgz
.shasum: 658a0d8ffbbc963990405bd2279fcd2b452be089
.integrity: sha512-o3E5KHHiHdaiwCJG3+9r70sncRKki71Ktf/TfXdW6myu+53rtZ56uLl5ylkQiCf60V3COJuOeekcxXVsjQ7cBA==
.unpackedSize: 2.6 MB

dependencies:
bson: ^5.4.0   kareem: 2.5.1  mongodb: 5.7.0 mpath: 0.9.0   mquery: 5.0.0  ms: 2.1.3      sift: 16.0.1   

maintainers:
- aaron <aaron.heckmann+github@gmail.com>
- rauchg <rauchg@gmail.com>
- tjholowaychuk <tj@vision-media.ca>
- vkarpov15 <val@karpov.io>

dist-tags:
5x: 5.13.20      latest: 7.4.1    legacy: 6.11.4   next: 7.0.0-rc0  unstable: 3.9.7  

published 4 days ago by vkarpov15 <val@karpov.io>

Installing a Specific Package Version#

  • If we only use the command npm install <packageName> then we always install the latest package version. In case, we want to install a specific version of a package. We can use the command below.
1
npm install <packageName>@<version>
1
npm install mongoose@5.3.0

Updating Local Packages#

  • Now, There is a case that we want to know newer versions of dependencies that we are using in our application to upgrade them. So firstly, we need to check the what are outdated packages and what are newer versions, we can use the command below.
1
npm outdated
1
2
3
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm outdated
Package  Current  Wanted  Latest  Location           Depended by
bson       5.3.0   5.4.0   5.4.0  node_modules/bson  npm-demo
  • As you can see the Current is our current dependency version, the Wanted is the version that we can update to, the Latest is the latest version of the dependency.

  • Now we can use the command below to update them.

1
npm update
1
2
3
4
5
6
7
8
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm update

removed 2 packages, changed 1 package, and audited 26 packages in 7s

1 package is looking for funding
  run `npm fund` for details

found 0 vulnerabilities
1
2
3
4
5
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm list
npm-demo@1.0.0 /home/duc/study/nodejs-backend/udemy/npm-demo
├── bson@5.4.0
├── mongoose@7.4.1
└── underscore@1.13.6

Note: the command npm update only works for upgrading Minor and patch versions as showed in the column Wanted of npm outdated command. For Major version it doesn't work.

  • If we want to upgrade the Major version, let's do following steps.
  • Install npm-check-updates to global
1
npm install -g npm-check-updates
  • Next, run the command below to check outdated packages in our npm application.
1
npm-check-updates
1
2
3
4
5
6
7
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm-check-updates 
Checking /home/duc/study/nodejs-backend/udemy/npm-demo/package.json
[====================] 3/3 100%

 bson  ^4.7.2    ^5.4.0

Run ncu -u to upgrade package.json
  • Now, to update the version, we can run the command below.
1
ncu -u 
1
2
3
4
5
6
7
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ ncu -u
Upgrading /home/duc/study/nodejs-backend/udemy/npm-demo/package.json
[====================] 3/3 100%

 bson  ^4.7.2    ^5.4.0

Run npm install to install new versions.
  • Now, if we look into the package.json, we can see the dependency version is updated.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
  "name": "npm-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bson": "^5.4.0",
    "mongoose": "^7.4.1",
    "underscore": "^1.13.6"
  }
}
  • Finally, we just need to run command npm install to update the dependency.
1
2
3
4
5
6
7
8
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm install

removed 5 packages, changed 1 package, and audited 26 packages in 545ms

1 package is looking for funding
  run `npm fund` for details

found 0 vulnerabilities
1
2
3
4
5
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ ncu
Checking /home/duc/study/nodejs-backend/udemy/npm-demo/package.json
[====================] 3/3 100%

All dependencies match the latest package versions :)

Dev Dependencies#

  • As you can see, all dependencies that we are practicing are application dependencies like mongoose and underscore  So our application needs these dependencies in order to function properly, but sometimes we use dependencies that are only used during development. For example, we have tools for running unit tests, we have tools for doing static analysis on our code, we have tools for bundling our JavaScript code and so on.
  • These dependencies are development dependencies. And they should not go in a production environment where we deploy our application. In this case we can use the command below.
1
npm install <packageName> --save-dev
1
npm install jshint --save-dev
  • The jshint is a static analysis tool for JavaScript code. It basically analyzes our JavaScript code, and looks for potential problems or syntactical errors.
  • After execute the command above, then we can see in our package.json. The jshint dependency is stored in property devDependencies and this mean the jshint is the development dependency and it should not go in the production environment.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
  "name": "npm-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bson": "^5.4.0",
    "mongoose": "^7.4.1",
    "underscore": "^1.13.6"
  },
  "devDependencies": {
    "jshint": "^2.13.6"
  }
}
  • If we look at node_modules we still see the jshint package there. So all dependencies whether they are application dependencies or development dependencies they are stored inside of the node_modules folder. They are only segregated in package.json.

Uninstalling A Package#

  • To uninstall a package, we can use the command below.
1
npm uninstall <packageName>
1
npm uninstall bson
package.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
  "name": "npm-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongoose": "^7.4.1",
    "underscore": "^1.13.6"
  },
  "devDependencies": {
    "jshint": "^2.13.6"
  }
}
  • After executing the command then we can see in the package.json the dependency is removed as well as in the node_modules folder.

Working With Global Packages#

  • So dependencies that we are using in the package.json are particular packages in the project npm-demo but there are packages on npm registry that are not specific node packages on npm registry that are not specific to an application. These are often command line tools that you want to access from everywhere. They're not tied to a specific folder, or a specific project. npm is an example of one of these global packages. It's a command line tool, you can run it from any folder. It's not specific to a given project. Another popular command line tool isAngular CLI. We use this to create a new Angular project.

  • If we want to install a Node package globally we can use -g flag.

  • For example, to install a package to global we can add -g to npm install as below.
1
npm install -g <packageName>
1
npm install -g npm@9.5.1
  • Or when we want to check outdated of global, we can use to npm -g outdated
1
2
3
4
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm -g outdated
Package   Current  Wanted  Latest  Location               Depended by
corepack   0.17.0  0.19.0  0.19.0  node_modules/corepack  global
npm         9.5.1   9.8.1   9.8.1  node_modules/npm       global
  • Or if we want to uninstall a global package we can use npm uninstall -g <packageName>.

Publishing A Package#

  • To publish a node package to publish repository, firstly you should register an account at npmjs.com which is the default publish registry that we have.
  • Then in our project, let's login with created account by using commands below.
1
npm login
  • Then follow the instruction in the command line to login.
  • Next, let's make sure that the name of your package is unique, so let's check the package.json with the property name and change it to make sure it is unique.
package.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
  "name": "duc-npm-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongoose": "^7.4.1",
    "underscore": "^1.13.6"
  },
  "devDependencies": {
    "jshint": "^2.13.6"
  }
}
  • Finally, use command npm publish to publish our node application.
1
npm publish
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm publish
npm notice 
npm notice 📦  duc-npm-demo@1.0.0
npm notice === Tarball Contents === 
npm notice 95B  index.js    
npm notice 339B package.json
npm notice === Tarball Details === 
npm notice name:          duc-npm-demo                            
npm notice version:       1.0.0                                   
npm notice filename:      duc-npm-demo-1.0.0.tgz                  
npm notice package size:  380 B                                   
npm notice unpacked size: 434 B                                   
npm notice shasum:        25f412387d49b442e93741f17ba09e770b33607e
npm notice integrity:     sha512-XUQGLgbVkTfzu[...]7/gPHcAKdZokA==
npm notice total files:   2                                       
npm notice 
npm notice Publishing to https://registry.npmjs.org/ with tag latest and default access
+ duc-npm-demo@1.0.0
  • Now, let's go back to npmjs.com and search our published package, we can see it is published successfully and other project can use it as a dependency.

 #zoom

Updating A Publish Package#

  • Okay so in the case we add some new features into our npm application and we want to publish it again. If we continue to use npm publish then you will get the error below.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm publish
npm notice 
npm notice 📦  duc-npm-demo@1.0.0
npm notice === Tarball Contents === 
npm notice 95B  index.js    
npm notice 339B package.json
npm notice === Tarball Details === 
npm notice name:          duc-npm-demo                            
npm notice version:       1.0.0                                   
npm notice filename:      duc-npm-demo-1.0.0.tgz                  
npm notice package size:  380 B                                   
npm notice unpacked size: 434 B                                   
npm notice shasum:        25f412387d49b442e93741f17ba09e770b33607e
npm notice integrity:     sha512-XUQGLgbVkTfzu[...]7/gPHcAKdZokA==
npm notice total files:   2                                       
npm notice 
npm notice Publishing to https://registry.npmjs.org/ with tag latest and default access
npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/duc-npm-demo - You cannot publish over the previously published versions: 1.0.0.
npm ERR! 403 In most cases, you or one of your dependencies are requesting
npm ERR! 403 a package version that is forbidden by your security policy, or
npm ERR! 403 on a server you do not have access to.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/duc/.npm/_logs/2023-07-29T03_29_40_095Z-debug-0.log
  • It is because the duc-npm-demo already published the version 1.0.0 so we can't continue to publish this version.
  • To handle this issue we have to update the property version in the package.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
  "name": "duc-npm-demo",
  "version": "1.1.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongoose": "^7.4.1",
    "underscore": "^1.13.6"
  },
  "devDependencies": {
    "jshint": "^2.13.6"
  }
}
  • Or we can use the command below to update it.
1
npm version <major/minor/patch>
1
npm version minor
1
2
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm version minor
v1.1.0
  • Then now, we can use command npm publish to publish our node application again.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
duc@duc-MS-7E01:~/study/nodejs-backend/udemy/npm-demo$ npm publish
npm notice 
npm notice 📦  duc-npm-demo@1.1.0
npm notice === Tarball Contents === 
npm notice 95B  index.js    
npm notice 339B package.json
npm notice === Tarball Details === 
npm notice name:          duc-npm-demo                            
npm notice version:       1.1.0                                   
npm notice filename:      duc-npm-demo-1.1.0.tgz                  
npm notice package size:  379 B                                   
npm notice unpacked size: 434 B                                   
npm notice shasum:        30c0be7feb9c51a10b0c920375b1288762e9118d
npm notice integrity:     sha512-xfQgeQkl44Uvt[...]FJaZAHCp9/v3A==
npm notice total files:   2                                       
npm notice 
npm notice Publishing to https://registry.npmjs.org/ with tag latest and default access
+ duc-npm-demo@1.1.0

 #zoom

See Also#

References#