Demo setup test package
This example uses two folders for demonstration. One called package where the code for the package lives and one called test-folder where scripts can be created for testing out the package.
When running test this was done on Mac OS
- Initialize with
npm initto init the package - You can use
npm init --scope=<yourscope>if you want to create a scope for you package - Create the
index.jsfile that will contain your code - Run
npm linkto link
If you get errors trying to run npm link you may need to configure some things. I had to do the following
mkdir "${HOME}/Packages/.npm-global"
npm config set prefix "${HOME}/Packages/.npm-global"
export PATH="${HOME}/Packages/.npm-global/bin:$PATH"
source ~/.zshrc
npm config get prefix
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}Note
On a Max using CTRL+SHIFT+. to see a hidden folder, since the name of folder in this example starts with a . it is hidden by default. You can toggle this in Finder using CTRL+SHIFT+.
If you get an error running source ~/.zshrc you may need to create this using the following steps
touch ~/.zshrc
nano ~/.zshrc
export PATH="${HOME}/Packages/.npm-global/bin:$PATH"Press CTRL+X, Y then Enter
Reload with source ~/.zshrc
- Create a
script.jsfile in thetest-folder - Add a
requirestatement to require your module
Important
Use the package name used when running npm init
- Now you need to link to your package from the test folder
cd test-foldernpm link <your package name>- At this point you will see a node_modules added to the test-folder
- Execute the script using
node script.js
- cd to the
packagefolder npm login- Run
npm publish --access publicif public - You can use scoped packages prefix with your scope