Node.js开发环境及应用实例

node.jsnpmrundev

Node.js 简练,清爽

如何在IDEA中加入默认/内置函数的auto-completion
IDEA node.js 函数的autocomplete

实现了批量编辑文件名含有netsuite的所有文件,添加tags 和 categories

实现了月初的需求:寻求批量分类解决方案
这下面的代码可以灵活拓展,应用在不同的批量修改编辑磁盘目录下的文件内容。

Node.js源代码如下:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

var fs = require("fs");
var buf = new Buffer.alloc(1024);

console.log("查看 _posts 目录");
const strPostDir = "/Users/carlzeng/AppTesting/blog/source/_posts";
var intUpdatedCnt = 0;

fs.readdir(strPostDir, function (err, files) {
if (err) {
return console.error(err);
}

try {
files.forEach(function (file) {
// console.log( file );
// if (file == '0模版.md'){
if (file.toLowerCase().indexOf('netsuite') != -1) {
console.log(file);

//读取文件,并且替换文件中指定的字符串
let replaceFile = function (filePath, sourceRegx, targetStr) {
fs.readFile(filePath, function (err, data) {
if (err) {
return err;
}
let str = data.toString();

// console.info(str);

str = str.replace(sourceRegx, targetStr);

// console.info("AFTER: \n" + str);
fs.writeFile(filePath, str, function (err) {
if (err) return err;
});

});
}

replaceFile(strPostDir + '/' + file, /tags: \[\]/, "tags: [NetSuite] \ncategories: NetSuite")
intUpdatedCnt++;

if (intUpdatedCnt == 2000)
throw new Error("EndIterative");
}

});
} catch (e) {
if (e.message != "EndIterative") throw e;
}
});

Sublime Text 编辑器 打开文件夹目录

Sublime Text 编辑器 打开文件夹目录,它便会加载这个文件夹下的所有文件,(用这种方式更新文章,非常便捷)
然后使用菜单“查找 》 替换”来搜索特定的特征的字符串.
搜索内容,也是支持正则表达

Node.js Dev 步骤

  1. Git clone

    1. 用IDE编辑器(比如:WebStorm)git clone下来
  2. 打开IDE,Terminal Tab会自动切换到clone下来的当前nodejs目录

    1. npm install
      1. 如遇到版本问题(比如本机运行环境的版本太低)就去下载安装新版,或者直接把node可执行文件放到/usr/local/bin目录下(MAC环境,我放了四个文件版本就升级了:node,npm,npx,corepack)
    2. npm run dev
      1. 如遇到问题,可能需要安装特定的包,比如:npx patch-package
    3. [可选]npx vite build
  3. 可直接编辑Node JS代码

  4. 网络环境的情况下npm命令前面可能需要爬墙:

    1
    HTTPS_PROXY="http://127.0.0.1:7890" 
  5. 调试

    1
    2
    3
    4
    5
    6
    #On MacOS or Linux, run the app with this command:
    DEBUG=myapp:* npm start

    #On Windows Command Prompt, use this command:
    set DEBUG=myapp:* & npm start

Node.js Build 步骤

编译到当前项目的dist目录中: npm run build

详细步骤见

Node.js项目打包和发布的实现方法

Docker编译

​ docker build -t ipserver .

版本更新后的npm run build

运行 npm run build 后提示报错

1
2
npm warn cli npm v11.0.0 does not support Node.js v20.10.0. This version of npm supports the following node versions: `^20.17.0 || >=22.9.0`. You can find the latest version at https://nodejs.org/.
11.0.0

升级

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
npm install -g npm
npm warn cli npm v11.0.0 does not support Node.js v20.10.0. This version of npm supports the following node versions: `^20.17.0 || >=22.9.0`. You can find the latest version at https://nodejs.org/.
npm error code EACCES
npm error syscall rename
npm error path /usr/local/lib/node_modules/npm
npm error dest /usr/local/lib/node_modules/.npm-i9nnxROI
npm error errno -13
npm error [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -> '/usr/local/lib/node_modules/.npm-i9nnxROI'] {
npm error errno: -13,
npm error code: 'EACCES',
npm error syscall: 'rename',
npm error path: '/usr/local/lib/node_modules/npm',
npm error dest: '/usr/local/lib/node_modules/.npm-i9nnxROI'
npm error }
npm error
npm error The operation was rejected by your operating system.
npm error It is likely you do not have the permissions to access this file as the current user
npm error
npm error If you believe this might be a permissions issue, please double-check the
npm error permissions of the file and its containing directories, or try running
npm error the command again as root/Administrator.
npm error A complete log of this run can be found in: /Users/carlzeng/.npm/_logs/2025-02-04T00_15_58_385Z-debug-0.log

切换到sudo -i 管理员用户, 重新尝试更新

HTTPS_PROXY=”socks5://127.0.0.1:7890” npm update -g

https://bun.sh/

Bun is a fast JavaScript
all-in-one toolkit
Develop, test, run, and bundle JavaScript & TypeScript projects—all with Bun. Bun is an all-in-one JavaScript runtime & toolkit designed for speed, complete with a bundler, test runner, and Node.js-compatible package manager. Bun aims for 100% Node.js compatibility.

1
2
3
4
5
6
7
curl -fsSL https://bun.sh/install | bash

//如果下载失败就换成(前提是您有可以*墙的环境)
HTTPS_PROXY="socks5://127.0.0.1:7890" curl -fsSL https://bun.sh/install | bash

//还是失败, 就换成:
HTTPS_PROXY="socks5://127.0.0.1:7890" curl -fsSL https://bun.sh/install | HTTPS_PROXY="socks5://127.0.0.1:7890" bash

运行成功了, 但是环境没有配置好

1
2
3
4
5
 bun                                                                      
dyld[968]: Symbol not found: _ubrk_clone
Referenced from: /Users/carlzeng/.bun/bin/bun
Expected in: /usr/lib/libicucore.A.dylib
zsh: abort bun

Node.js的Docker容器

目前完还不会打一整个docker的包,先搞一个Node.js的Docker容器,方便管理

1
2
3
4
5
6
7
8
9
10
11
12
13
version: "3"
services:
node:
stdin_open: true
tty: true
container_name: nodejs
restart: always
volumes:
- './app:/home/app'
ports:
- 3001:3000
image: node
command: bash

本小节思路来源:Mac上安装Node.js的Docker容器

Node.js的Docker Deployment步骤

  1. docker exec -it nodejs bash
  2. [可选步骤,仅适用于升级发布]
    1. ps -falx | head -1; ps -falx | grep ‘npm|node’
    2. kill -9 「node进程id,PPID值」
  3. node /home/app/blogsearch/bin/www &
    1. 在命令后面添加一个 &,让node运行在后台;回车以后,还有机会输入其他命令
    2. nohup node /home/app/blogsearch/bin/www &
      1. 回车以后,还有机会输入其他命令; 退出 docker exec -it 里面的bash后,程序还在后台运行

疑问:如何让这个node常驻在后台执行,意思是:当按键control+c退出当前docker exec -it nodejs bash时,不要退出这个进程。


已上线2.0版本

实测:Adding full text Search via FlexSearch to a Blog

起步:https://expressjs.com/en/starter/installing.html

1
2
3
4
5
6
7
8
9
10
11
12
13
mkdir blogsearch

cd blogsearch

npm init
entry point: (index.js) server.js

HTTPS_PROXY="http://127.0.0.1:7890" npm install express
added 62 packages, and audited 63 packages in 10s


HTTPS_PROXY="http://127.0.0.1:7890" npm install --save flexsearch
added 1 package, and audited 64 packages in 3s
1
2
3

const { Index, Document, Worker } = require("flexsearch");
const index = new Index();

思路来源:Adding full text Search via FlexSearch to a Blog

## 本地node程序的测试

备注: 本地node程序的测试方式为:

  1. 在WebStorm > Terminal中运行: npm start
  2. 打开浏览器访问: localhost:3000
  3. 编辑修改的jade模版会实时展现在本地网页localhost:3000中
  4. app程序的修改部分, 需要重新启动/运行npm start

个性化需求沟通 扫客服加V加群: