10 examples of 'npm update single package' in JavaScript

Every line of 'npm update single package' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
77function npmUpdate(dependencies) {
78 return new Promise(function (resolve, reject) {
79 npm.commands.update(dependencies || [], function () {
80 resolve();
81 });
82 });
83}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

36async function updatePackage(pkgPath) {
37 const currentPkg = await readPackage(pkgPath);
38 const dependencies = getDependencies();
39
40 logger("Update package", chalk.cyan(pkgPath));
41
42 currentPkg.dependencies = updateVersions(currentPkg.dependencies, dependencies);
43 currentPkg.devDependencies = updateVersions(currentPkg.devDependencies, dependencies);
44
45 writePackage(pkgPath, currentPkg);
46}
60function update(updateInfo, callback) {
61 var metadata = updateInfo.metadata;
62 var version = updateInfo.version;
63 var projectPath = metadata.path;
64
65 if (version) {
66 logger.info('Updating %s to %s', metadata.name, version);
67
68 var cleanVersion = cv(version);
69 var pm = packageManagers.get(metadata.packageManager);
70 var packageId = metadata.packageManager === 'github' ? metadata.repo : metadata.name;
71
72 var packagesTempPath = path.join(config.get('tmp'));
73
74 var from = path.join(packagesTempPath, metadata.name, cleanVersion);
75 var to = path.join(projectPath, cleanVersion);
76
77 pm.install(packageId, version, from, function (err) {
78 if (err) {
79 logger.warn('Failed to download and install package', { name: metadata.name, version: version, error: err});
80
81 callback(err);
82 return;
83 }
84
85 util.copy(from, to, metadata.files, function (files) {
86
87 logger.info('Copied %d files', files.length, {name: metadata.name, version: version, files: _.map(files, 'path')});
88
89 if (files.length === 0) {
90 logger.warn('Something might be wrong with update.json', { name: metadata.name, version: version });
91
92 remove(to, _.noop);
93 callback(null, _.extend(updateInfo, { updated: false }));
94 } else {
95 callback(null, _.extend(updateInfo, { updated: true, updatePath: to }));
96 }
97 });
98 });
99 } else {
100 logger.debug('Skipping project %s update, already at latest version', metadata.name);
101
102 callback(null, _.extend(updateInfo, { updated: false }));
103 }
104}
47async installPackage(pkg: string, dev?: boolean) {
48 const {
49 filesystem: { exists },
50 print,
51 system
52 } = this.context
53
54 const count = pkg.split(' ').length
55 const w = `${dev ? 'development ' : ''}package${count > 1 ? 's' : ''}`
56 const spinner = print.spin(`Adding ${w} into project...`)
57
58 const yarn = exists('yarn.lock') === 'file'
59 const cmd = `${yarn ? 'yarn add' : 'npm install'} ${
60 dev ? '-D' : yarn ? '' : '-S'
61 } ${pkg}`
62 await system.run(cmd)
63 spinner.succeed(`${count} ${w} were successfully added.`)
64}
50function _npm_package()
51{
52 console.log('Preparing package.json...');
53
54 const package_path = path.resolve(dist_path, 'package.json');
55 const package_json = JSON.parse(fs.readFileSync(path.resolve(module_path, 'package.json')).toString());
56 delete package_json.private;
57 const keep_scripts = ['install', 'build'];
58 const scripts = {};
59 for(const script of keep_scripts)
60 scripts[script] = package_json.scripts[script];
61 package_json.scripts = scripts;
62 delete package_json.devDependencies;
63 fs.writeFileSync(package_path, JSON.stringify(package_json, null, 4));
64
65 console.log('Prepared!');
66}
282function updateLocalDependency(packageFolder: PackageFolder, dependencyName: string, getNewDependencyVersion: (dependencyName: string) => string | undefined): boolean {
283 const newDependencyVersion: string = getNewDependencyVersion(dependencyName) || "";
284
285 const packageFolderPath: string = packageFolder.folderPath;
286
287 const localDependencyUpdated = updateGeneratedPackageDependencyVersion(
288 resolvePath(packageFolderPath, "package.json"),
289 dependencyName,
290 newDependencyVersion);
291
292 updateGeneratedPackageDependencyVersion(
293 resolvePath(packageFolderPath, "README.md"),
294 dependencyName,
295 newDependencyVersion);
296
297 if (packageFolder.extraFilePaths) {
298 for (const extraFilePath of packageFolder.extraFilePaths) {
299 updateGeneratedPackageDependencyVersion(
300 resolvePath(packageFolderPath, extraFilePath),
301 dependencyName,
302 newDependencyVersion);
303 }
304 }
305
306 return localDependencyUpdated;
307}
26installNpmPackage(name: string, version: string | null | undefined, cwd: string = this.scopes.localPath) {
27 const versionWithDelimiter = version ? `@${version}` : '';
28 const cmd = `npm i --save ${name}${versionWithDelimiter}`;
29 return this.command.runCmd(cmd, cwd);
30}
24function updatePackageJson(version) {
25 fs.writeFileSync(
26 PACKAGE_JSON_PATH,
27 JSON.stringify(Object.assign({}, packageFile, { version }), null, 2) + '\n'
28 );
29}
43function updatePackageVersion(version) {
44 var packageJson = loadPackageJson();
45 packageJson.version = version;
46 fs.writeFileSync('package.json', JSON.stringify(packageJson, null, ' '));
47}
46public async add(workingDirectory: string, packageName: string, version: string, isDev: boolean): Promise<void> {
47 this._display.info("Adding package...");
48 return new Promise<void>((resolve, reject) => {
49 const config: { [id: string]: any } = {};
50 config.prefix = this._fileSystem.pathFormat(workingDirectory);
51 if (isDev) {
52 config["save-dev"] = true;
53 } else {
54 config["save-prod"] = true;
55 }
56 npm.load(config, (err, result) => {
57 if (err) {
58 reject(err);
59 } else {
60 npm.commands.install([packageName + "@" + version], (err2, result2, result3, result4, result5) => {
61 if (err2) {
62 reject(err2);
63 } else {
64 resolve();
65 }
66 });
67 }
68 });
69 });
70}

Related snippets