This is the last article on NPM Publish Package, summarizing the key points to be aware of during development and the set of errors I have encountered so far.


No long words, full of key points to solve the problem, easy to quickly find the problem and solution.


01 – npm link

The premiseAnother project is needed to work with it, and Link can simulate exactly what happens after install is complete.


PROJECT
├─ Cat-Web – Storage
└─ testModules


cd PROJECT\cat-web-storage
npm linkCopy the code
This step is to generate the link package in NodeJs\node_global\node_modules.


cd PROJECT\testModules
npm link cat-web-storageCopy the code
This step is to add PROJECT\cat-web-storage link to the current PROJECT.






Link and pointing relationship


The end result is this
TestModules node_modules directly points to cat-web-storage source code. This directly reduces the frequency of building projects that need to be developed,
Save time and effort!!



02 – package.js

I want to be able to do it directly
Console. log, debugger, or breakpoint debuggingYou need to modify the package.js configuration in the cat-web-storage project.


Development stage:
Modify the configuration as follows. The main and Module entries should point to the source file instead of the build file.

{
  "main": "src/index.ts"."module": "src/index.ts"
}Copy the code



Confirm need to publish:
Notice that you need to change it to
buildAfter the file as the entry point,
Remember that! Remember that! Remember!!

{
  "main": "dist/web-storage.common.js"."module": "dist/web-storage.esm.js"
}Copy the code



03 – resolve.symlinks

Resolve. Symlinks. After configuration, webpack will compile cat-web-storage source code.

chainWebpack: config => {
  config.resolve.symlinks(false);
}Copy the code


These are the changes that need to be made during development.
Very important!!





04 – Dependency errors cannot be found

If the following error occurs, check whether testModules has been installed
The correct linkCat-web-storage source code!

This dependency was not found:
* cat-web-storage in ./src/main.ts
To install it, you can run: npm install --save cat-web-storage
Type checking and linting in progress...Copy the code



05 – ESLint configuration error not found

If the following error occurs, check whether the
resolve.symlinksConfigure into the testModules project!

ERROR Failed to compile with 1 errors
  Module build failed (from ./node_modules/eslint-loader/index.js):
  Error: No ESLint configuration found in PROJECT\cat-web-storage\dist.Copy the code



06 – No attribute warning error exists

If you’re developing in TypeScript, be aware of this.
TestModules starts normally, but the console warns, but the page runs normally.
Common as follows:

Property '$localStorage' does not exist on type 'WebStorage'.

this.$localStorage.set('number', 10086); // Case one ^ vm.$localStorage.set('number', 10086); // Case 2 ^ window.$localStorage.set('number', 10086); // Case three ^Copy the code

This warning error is due to the absence of a type description file, i.e
*. Which s file. SRC in the testModules project
Create any *.d.ts fileTo add the following information:


Import {LocalStorage, SessionStorage, CookieStorage} from'cat-web-storage'; // Declare the exported interface description type as an interface to vUEtypeIn the description file //'vue/types/vue'Vue type Description File pathdeclare module 'vue/types/vue' {
  interface Vue {
    $localStorage: LocalStorage
    $sessionStorage: SessionStorage
    $cookieStorage: CookieStorage
  }
}Copy the code



Restart the project to solve the problem.


Do not forget to switch the NPM source to NPMJS when you publish package. NPM mirror CNPM of Ali Cloud will be synchronized every 10 minutes.


The last

I had a good time trying to write an NPM publish package. At the beginning, I also read some data and wrote. There are two main places where the consumption of time is more.


The biggest problem with rollup + TypeScript is that the web is relatively small and old. Some of the packages used are no longer maintained.


ESLint configuration error not found, it took some time here, mainly the first time I encountered an ESLint error, I thought I didn’t introduce ESLint package, then I introduced ESLint and configured rules, But in the end it failed to start. Then I thought, why is ESLint related when I use TypeScript for both my release and test projects? After several rounds of keyword search, I finally found an article with similar problems to mine, including the key information resolve. Symlinks.


And last but not least, good luck to you guys


Code force Max!


Productivity is bursting!


9 to 5 no overtime!




Copyright Notice:
The copyright of this article belongs to the author Lin Xiaoshuai, shall not be reproduced without authorization and second modification.
Reprint or cooperation, please leave a message and contact information in the background.


Related articles
  • [NPM publish Package] Publish process

  • [NPM publish Package] Test process